I want to dynamically add a morphToMany Relation between models (e.g. User, Attachment, ...) and a File Model based in a File Package. The ServiceProvider should build the relation.
How do I do that ?
Here's the code I've tried already:
$fileables = config('topredmedia-filemanager.fileable');
foreach ((array)$fileables as $modelType) {
if (class_exists($modelType)) {
$model = App::makeWith($modelType);
// Add Relation to all specified models in config
Builder::macro('files', function () use ($model) {
return $model->morphToMany(File::class, 'fileable', 'trm_fileables');
});
}
}
BTW: When I run this code but with a morphMany relation everything works fine.
When I execute $model->files()->sync([1, 2, 3]) in a controller the pivot table (file_id, fileable_id, fileable_type) should be filled with the right data.
But when I run it I get the following error: "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'fileable_id' cannot be null (SQL: insert into trm_fileables (file_id, fileable_id, fileable_type) values (3, , Path\To\Model\User))"
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire