laravel / nova-issues

554 stars 35 forks source link

HasMany with Attach #520

Closed sjmarve closed 6 years ago

sjmarve commented 6 years ago

Is there any way to have the HasMany field show Attach instead of the create.

dillingham commented 6 years ago

You can change it in your language file But it makes no sense to do so.

rickmacgillis commented 6 years ago

@dillingham - I think he means the functionality. I've wondered the same thing.

davidhemphill commented 6 years ago

Why would you want this?

rickmacgillis commented 6 years ago

@davidhemphill It's the same difference between "editing" and "creating" as this situation. If the model already exists then we need the ability to attach it rather than create a new one altogether.

sjmarve commented 6 years ago

@davidhemphill say for example if you have a Company Model and a User Model where its a one to many relationship, the desired ability is to be able to "Attach" existing users to the company model (and also have the current functionality that creates users in this scenario). Hope this makes sense

rickmacgillis commented 6 years ago

Was this implemented?

CT-Web commented 5 years ago

It would be great if this feature were proposed.

rovansteen commented 5 years ago

@taylorotwell care to explain why this is closed (and thus probably won't be implemented)?

NioTeX commented 5 years ago

Does anyone have an update on this?

i-bajrai commented 5 years ago

use the BelongsToMany field rather than hasMany and problem solved!

carnevalle commented 5 years ago

I have several use cases where this is needed.

I have a service aggregating news from different websites. Some of these articles are derived articles and I want to group them under the original article. Thus I want to attach an existing article (child) to an existing article (parent). I would never create an article myself as these are synced automatically.

I could go for a BelongsToMany, but then I would create a Many to many relationship instead og af One to many, which doesn't seem ideel.

niladam commented 5 years ago

I don't understand why this has been closed ?

Imagine the following:

I have a Category model.

The model has a parent relationship with itself:

    public function parent()
    {
        return $this->belongsTo(Category::class, 'parent_id');
    }

Obviously, using this i can set a parent category. I also have a children relationship like this:

    public function children()
    {
        return $this->hasMany(Category::class, 'parent_id');
    }

Of course, i'm planning on having the children and parent on the same table. I see no reason in using a separate table for this. Also, BelongsToMany doesn't make sense in here either.

How would one work this out ?

techouse commented 5 years ago

Any development on this matter? v2.0.8 still has this issue.

rickmacgillis commented 5 years ago

@i-bajrai might be right, @techouse. I think I tried it once and it worked, even though it "feels" wrong doing it that way since it's not an accurate description of the relationship.

techouse commented 5 years ago

Yes I know that a many-to-many way works, but that’s not really what I want in this case.

ohchiko commented 5 years ago

I have a similliar issue.

I have a Product and Material model. A single Product need at least one Material to be created, while a Material can be created without having a Product. But a Product may have many Materials and a Material may only have one Product (each model has a batch column as identifier). So I'm using the one-to-many relationship.

class Product extends Model
{
    public function materials()
    {
        return $this->hasMany(Material::class);
    }
}
class Material extends Model
{
    public function product()
    {
        return $this->belongsTo(Product::class);
    }
}
class ProductController extends Controller
{
    public function store(Request $request)    // the $request contains 'data' to create and 'materials' to attach (or something)
    {
        $product = Product::create($request->input('data'));

        // I'm trying to make a relation between the created product and the existing materials
        $product->materials()->associate($request->input('materials'));    // the 'materials' contains ids of existing materials.

        return new ProductResource($product);
    }

Does the one-to-many relationship did not appropriate in this case? Which relationship should I use?

Edit: Or maybe I have to use has many through relationship?

This is what's in my mind, imagine a burger as the Product and bread, beef, tomato, etc as the Materials. So, in order to make a burger we need those materials. But we could have already stocked these materials in the warehouse (but each have different id, ex. there maybe many breads but with different id), so when we got an order of a burger, we take those materials out of the warehouse and our stock of that materials is reduced. I need to record this, what materials does the burger use.

I think I'm wrong in understanding the relationship to use.

Grunkhead commented 5 years ago

Same issue here.

The category which I am watching is a category which has a single parent. This category is also a parent of 2 other categories. So it is the category in between. My 'HasMany' field keeps loading forever.

Here the fields which a have added to the function 'fields' with in the Category Nova Resource. image

Endless loading here. image

What goes wrong? How can I or 'we' solve this? :)

Grunkhead commented 5 years ago

UPDATE;

When I went back to the Laravel Nova index, where you land on after login. It seems the network tab resetted itself. After that I tried to directly watch the category again and the child categories in the 'HasMany' tab loaded correctly.

image

But when I clicked on a child category of that Category. The 'HasMany' field keeps loading forever again. image

pxlrbt commented 4 years ago

@taylorotwell This issue is still not fixed. Why is it marked as closed? Why is there no way to attach existing entries in a HasMany relationship?

nathan-io commented 4 years ago

Just another vote for this feature.

In our use case, we have a ServiceCategory model, which has many Services. Yet a Service only belongs to one ServiceCategory.

While creating a ServiceCategory, we'd like to be able to attach multiple Services to it.

The developer suggested altering the relationships so that a Service hasMany ServiceCategories. Yet this seems like a hack, and I'd prefer not to misrepresent the relationship between these entities just to bypass a limitation of Nova.

Like other commenters, I'm unsure why this issue was closed without explanation. This software is a fantastic value for the price - but it is not free.

alekmlynek commented 4 years ago

I'm creating an e-commerce application and am a little surprised this isn't a feature. When creating product views, you oftentimes want product variations attached to those views.

sofian-io commented 4 years ago

Would like to see an option for this too, maybe some way of passing the desired action type(s) to the HasMany::make field similar to withMeta?

HasMany::make('Name')->withAction([ 'attach', 'create' ]);

HasMany::make('Name')->withAction('attach');

infinitodk commented 4 years ago

Typical behaviour from Nova developers against paying customers. Closing valid requests without a single explanation and then just ignore them while gladly accepting their cash.

pxlrbt commented 4 years ago

@infinitodk Well, apparently they consider this repo as an bug tracker and close anything they labeled as a feature request. Still not good to leave everybody without a clue.

rickmacgillis commented 4 years ago

Typical behaviour from Nova developers against paying customers. Closing valid requests without a single explanation and then just ignore them while gladly accepting their cash.

Sadly that happens a LOT with all Laravel projects. I think it'd make a lot of people a lot happier if they simply asked people to make a PR and they'd test and deploy that fix, as it makes it more efficient for their team to not have to build hundreds of requests.

Perhaps we should beta test it and if one of us has time to make a PR, then see if they respond favorably to it. Unfortunately as I've moved on with other projects, I don't have an interest in making the PR myself. In the case of Nova, though, the code repo is here

pxlrbt commented 4 years ago

I tried to solve this issue but have not had the time to test it thoroughly and finish it yet. There are quite a few files involved in this.

chucklin72 commented 4 years ago

https://github.com/dillingham/nova-attach-many

But doesnt seem to work for one to many. perhaps this works for many to many.

aghayeff commented 4 years ago

I wondered why this problem is still actual? I have a User and UserPhones model. When I create a User Model, I need also to attach user's numbers. But Nova only allows to attach numbers after creating a User.

FutureFutureTo commented 4 years ago

So basically, for now, we just have to have a pivot table to make this work?

aghayeff commented 4 years ago

Using Observer we could solve this issue. Just create, input fields for HasOne or HasMany, then after creating an item, insert data to HasOne or HasMany table. Using mutators you could get data from HasOne, HasMany table in index, details page.

flatcapco commented 4 years ago

Using Observer we could solve this issue. Just create, input fields for HasOne or HasMany, then after creating an item, insert data to HasOne or HasMany table. Using mutators you could get data from HasOne, HasMany table in index, details page.

Still hacky though isn't it.

Just to add to the list of user frustrations;

I have Availabilities that I need to attach to a Booking - both exist... both relations will only ever need to be Attached with no option of creation.

BelongsTo and HasMany with "Attach" seems to be the only clean solution to handle the UI properly?

alekmlynek commented 4 years ago

+1 for this feature. I would take it a step further and say having tools like "clone" and "attach" would be a nice addition. For e-commerce, this is used many times. For address books, you would 'clone' an address then attach it to the order to preserve history. As it stands now, you must create a new address for the user from Nova instead of being able to copy from one they already own.

Line items and attaching products to them is another. There are many instances where I can think of attaching as being very useful where you need to lookup an existing data set.

schonhoff commented 3 years ago

I would love to see this feature. It would help me out so much.

niladam commented 3 years ago

@taylorotwell @davidhemphill can you guys please provide some feedback on this feature request ? Will this happen anytime soon ? Will it not ? If not, can you please suggest some alternative uses and/or maybe provide some hints on the situation ?

Thank you:)

navidnadali commented 3 years ago

We are really missing this feature too and do actually need it to the point that I'll have to take a day or two off the project to add this as a plugin.

I'm a bit surprised at the lack of responses from Laravel team in RE to this issue, There is over 30 paying customers in this thread complaining about a very valid issue with no response or comment.

I guess, as the community we will have to fill in as the guys are most likely incredibly busy... I'll share the spec once I go through the entire thread and will share the package too of course once done.

rela589n commented 3 years ago

Why would you want this?

Consider for example poll application.

Poll hasMany Questions hasMany AnswerOptions

Then, when you add new Question to poll (or edit an existing one), you would like to have at least 2 answer options. If you create empty question without answer options, then it is invalid! It doesn't comply invariants.

rela589n commented 3 years ago

Maybe you are looking for https://github.com/yassipad/laravel-nova-nested-form ?

schonhoff commented 3 years ago

It is not quite the same use-case described here @rela589n . I had a look at the package too as I had the "issue". In my case I want to set already existing vehicles to a user but you can't do that with Nova at the moment.

My relationship would be an user has many vehicles and a vehicle belongs to an user. On the user side I need to enter new vehicles and on the vehicle site I can choose from the already existing users. It would be better to choose on both sides or enter new entries on both sides. Maybe an create and an attach button can be implemented on the relationship view. I hope some day they will implement this feature.

FutureFutureTo commented 3 years ago

First off, I appreciate Nova as a product very much, along with the consistent updates and effort from the team.

A lot of people here keep pushing toward a many to many....

To change business logic to suit the restraints of Nova seems a little counterintuitive.

We're using Nova for as a tool for less tech inclined people to make edits and changes to the DB on our team.

I understand that the Nova team is busy, but this seems like a highly requested feature - for now, we will have to build something external to Nova to allow our team to attach resources in a HasMany - definitely more work, but less work and less annoying than changing our DB and business logic to create a many to many relationship where it's not needed.

I'm hoping that the Nova team will address this soon though :)

bernhardh commented 3 years ago

Not sure if such posts change anything. This issue is now 2,5 years old and there is no reaction at all. Not even a "no we want implement it because...."

I love nova and laravel - both are great products and therefore the creators of it must be great developers. But they should rethink there customer/community management and behaviour.

crynobone commented 3 years ago

I read through this issue a few times and it's hard to follow the actual requirements from outsider standpoint which doesn't have these requirement. I worried even if I come out with a PR it going to fixes some of the above problem or nothing at all.

There few mention 3rd party packages and it doesn't fit the requirement to some (if not all).

I would definitely think a well crafted set of real world examples with some visual might be useful if we want to move forward. Then we can see if everyone on the same page.

bernhardh commented 3 years ago

Thanks for answering! Thats a good reason, why not to implement it yet.

I can tell you my current real world usecase. I have recipes (cooking recipes) which (optionally) belongs to exactly one category (for example "fast food"). See the following code:

// Recipe model
class Recipe extends Model 
{
    public function category() 
    {
        return $this->belongsTo(\App\Models\Category::class, 'category_id', 'id');
    }
}

// Category model
class Category extends Model 
{
    public function recipes()
    {
        return $this->hasMany(\App\Models\Recipe::class, 'category_id', 'id');
    }
}

// Category Resource
class Category extends Resource 
{
    public function fields(Request $request) 
    {
        return [
            HasMany::make('Recipes', 'recipes', \App\Nova\Recipe::class),
            // ...
        ];
    }
}

The general usecase is, that users create new recipes and in the recipe form view, they would pick the category from the selectbox. Thats fine and thats already working. But now a user decides to add the new category "vegetarian". After creation, he want to attach recipes to this new category. He could of course go to the recipes form and add it there one by one. But it would be faster and nicer, to go to the new created category and just attach recipes to it.

crynobone commented 3 years ago

He could of course go to the recipes form and add it there one by one. But it would be faster and nicer, to go to the new created category and just attach recipes to it.

So from what I understand it is basically https://github.com/dillingham/nova-attach-many

schonhoff commented 3 years ago

Hello,

here is my real world problem:

// Vehicle model:
    /**
     * A vehicle belongs to a vehicle cost center
     *
     * @return BelongsTo
     */
    public function costCenter() : BelongsTo
    {
        return $this->belongsTo(
            'App\VehicleCostCenter',
            'cost_center_id'
        );
    }

// Vehicle ressource
    public function fields(Request $request)
    {
        return [
            BelongsTo::make(__('Cost Center'), 'costCenter', VehicleCostCenter::class),
            // ...
        ];
    }

// VehicleCostCenter model
    /**
     * A vehicle cost center has many vehicles.
     *
     * @return HasMany
     */
    public function vehicles() : HasMany
    {
        return $this->hasMany(
            'App\Vehicle',
            'cost_center_id'
        );
    }

// VehicleCostCenter ressource
    public function fields(Request $request)
    {
        return [
            HasMany::make(__('Vehicles'), 'vehicles', Vehicle::class),
            // ...
        ];
    }

A company (here VehicleCostCenter) has a many cars and a car belongs to a company. But the cars can be switched around in another company. So I deattach them in the one company and want to attach them in another. One vehicle isn't a problem because it can be done in the vehicle resource itself but with many changing at the end of a year it is quite a pain to do it one by one. So it would be nice to have an option in the company to attach the vehicles instead just creating a new vehicle.

Other examples that I would see the same problem are mentioned above. The package mentioned above is working for many to many relationships but I and others in this issue don't want to change a real world one-to-many relationship into a many-to-many relationship.

Hope it helped to understand the real problem.

Nova hasMany

Added a picture to help a little bit. In the red circle a "attach existing vehicle"-Button would be beautiful

crynobone commented 3 years ago

@schonhoff can you write out the flow how the process removing vehicles from previous company to a new company, so we can understand and maybe suggest an alternative approach. I don't see how that would fit the previous example.

It might be best to have a separated discussion under https://github.com/laravel/nova-issues/discussions so we can discuss it further.

pxlrbt commented 3 years ago

Opened a discussion here: https://github.com/laravel/nova-issues/discussions/3191

bernhardh commented 3 years ago

@crynobone

He could of course go to the recipes form and add it there one by one. But it would be faster and nicer, to go to the new created category and just attach recipes to it.

So from what I understand it is basically https://github.com/dillingham/nova-attach-many

I use this package for other things (for hasMany Relations), but in this case, its not they way to go.

1) First of all, I have over 30k of recipes in my database. The mentioned package shows all items at once in the edit field. That takes forever. But maybe thats something the package can/should address. 2) There is no option to view the relations on Detail view. I could of course play around with onlyOnForms for the AttachMany and create an extra HasMany with onlyOnDetail, but then there is once again the problem, that the user see the "Create" button on detail. 3) I had to "teach" the users of my nova installation, that they have to edit relationships not on the edit view, but on the detail view (thats how nova works in normal situations). That would break this rule.

albalooshi commented 3 years ago

@davidhemphill @crynobone Any updates on this request?

goromachine commented 3 years ago

No updates yet? this should be integrated in nova, not as a 3rd party package...

another example:

I have to create many Vehicles and this ones are related with a Serie (Audi, BMW, SEAT, etc). If the Serie is created one time, then I should be able to attach the Serie into the new Vehicles, right? I understand it as the Vehicle hasOne Serie, but the Serie BelongsToMany Vehicles.

Kaylakaze commented 3 years ago

The requirement is simple: an "Attach existing" button for the HasMany table/component that functions just like the one on the BelongsToMany table/component. It's not rocket science. There's no point in confusing everything talking about other packages and use cases. The functionality is already there for BelongsToMany. Add it to HasMany. Problem solved.