Closed francescomalatesta closed 3 years ago
I am using "Relation" as a field, but I am afraid isn't enough right now for the CRUD module
Hey @francescomalatesta
What do you mean by not enough? Can you describe what you would like to see?
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
Let me know if you need anything else that can help!
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.
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();
}
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.
thx thx thx, so very much
Hey @tabuna :) I am keep working on and writing about Orchid CRUD. I was wandering, what about ManyToMany relationships?
Imagine I have two resources:
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