I've used polymorphic relationship for a while, and the morph_type column stores many repeated string, and I'm afraid of if one day I need to refactor a model, then I might need to replace them from several tables.
That's say a simple morph relation(from laravel document)
posts
id - integer
name - string
users
id - integer
name - string
images
id - integer
url - string
imageable_id - integer
imageable_type - string
The imageable_type in images stores many 'App\Models\Blog\Post' and 'App\Models\Blog\User' strings.
The images table looks like this:
id|url |imageable_id|imageable_type
1 |http://|1 |App\Models\Blog\User
2 |http://|1 |App\Models\Blog\Post
3 |http://|2 |App\Models\Blog\User
4 |http://|3 |App\Models\Blog\Post
5 |http://|5 |App\Models\Blog\User
What if create another imageable_types table as a imageable_type reference?
imageable_types
id - integer
type - string
The imageable_types table looks like:
id|type
1 |App\Models\Blog\User
2 |App\Models\Blog\Post
And images table will be like:
id|url |imageable_id|imageable_type_id
1 |http://|1 |1
2 |http://|1 |2
3 |http://|2 |1
4 |http://|3 |2
5 |http://|5 |1
Does it better or worse?
If it is possible to do, how to modify model and not affecting the original code?
Like this:
$post = App\Models\Blog\Post::find(1);
$image = $post->image;
$image = App\Models\Blog\Image::find(1);
$imageable = $image->imageable;
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2UKkTRG
via IFTTT
Aucun commentaire:
Enregistrer un commentaire