orchidsoftware / crud

Simplify the process of building CRUD (Create, Read, Update, Delete) functionality in Laravel using the features of Orchid.
https://orchid.software
MIT License
138 stars 34 forks source link

ManyToMany relationships #22

Closed francescomalatesta closed 3 years ago

francescomalatesta commented 3 years ago

Hey @tabuna :) I am keep working on and writing about Orchid CRUD. I was wandering, what about ManyToMany relationships?

Imagine I have two resources:

  1. Movie
  2. Genre

and I want to be able to define attach genres to a movie. How would you proceed? Is this something in the plans?

Thanks a lot for your precious time

francescomalatesta commented 3 years ago

I am using "Relation" as a field, but I am afraid isn't enough right now for the CRUD module

tabuna commented 3 years ago

Hey @francescomalatesta

What do you mean by not enough? Can you describe what you would like to see?

francescomalatesta commented 3 years ago

Sorry, you're right. I should add more data.

So, as I said before, I have two models:

I defined the many to many relationship in the models. Then, I defined the relationship field in the MovieResource like

Relation::make('genres.')
                ->fromModel(Genre::class, 'name')
                ->multiple()
                ->title('Genres')

like I saw here: https://orchid.software/en/docs/field/#relation

When I try to create a new movie and assign a single genre, it says

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'genres' in 'field list' (SQL: update `movies` set `genres` = ["1"], `movies`.`updated_at` = 2021-01-15 22:25:57 where `id` = 2)

If I try to assign two genres, the error is

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'genres' in 'field list' (SQL: update `movies` set `genres` = ["1","2","3"], `movies`.`updated_at` = 2021-01-15 22:27:20 where `id` = 2)

Instead of trying to write on the pivot table, my guess is the package is trying to assign those values to a "genres" field that doesn't exist

francescomalatesta commented 3 years ago

Let me know if you need anything else that can help!

tabuna commented 3 years ago

The area of responsibility of the fields is much smaller than you might be used to. They do not, in any way, affect the behavior of the database or models. In reality, the error occurs only because there is no processing of this connection.

Resource Events

Each resource has two methods that do the processing, onSave and onDelete. Each of them is launched when the event is executed, and you can change or supplement the logic:

/**
 * Action to create and update the model
 *
 * @param ResourceRequest $request
 * @param Model           $model
 */
public function onSave(ResourceRequest $request, Model $model)
{
    $model->forceFill($request->all())->save();
}

/**
 * Action to delete a model
 *
 * @param Model $model
 *
 * @throws Exception
 */
public function onDelete(Model $model)
{
    $model->delete();
}
francescomalatesta commented 3 years ago

Super clear! Thanks and sorry if I am making a lot of questions. The more I use orchid the more I love it, so I want to switch my mentality from Nova to this one.

Have a nice day.

Oxicode commented 1 year ago

thx thx thx, so very much