Courses

Eloquent: The Expert Level (Laravel 9)

Polymorphic Relations: Explained with Example

Previous: Raw Queries as a Last Resort
avatar

what about down in migration file ? how to drop it?

avatar

Personally, I don't use down() method for a long time, Taylor said it back in 2017: /post/still-need-migrations-taylor-says-no

avatar
You can use Markdown
avatar

Let's assume there is a PhotoController for polymorphic child resource

to manipulate Photo data.

Assuming Photo data is updated, then parent row,

either Project or Task, should be updated as well.

I see several options.

Option 1:

To create ProjectPhotoControler and TaskPhotoControler.

This way when inside ProjectPhotoControler,

$photo->photoable

is Project and may address Project fields by -> operator.

And same for Task.

In this case there is some code repetition,

yet dedicated controler may be more readable.

Option 2:

To create PhotoControler only,

yet will need if blocks to compare to photoable_type, like that

$if ($photo->photoable_type === Project::class){ ...}
$if ($photo->photoable_type === Task::class){ ...}

In this case, there is only one controler.

Yet, if there are more Owner Entities,

a lot of "if condition" blocks can make a code not readable.

Question: Can you please reccomend?

avatar

It's your personal preference, but I would go with PhotoController and if-else turned into match

avatar
You can use Markdown
avatar

Hi, could you please share the repository link for this course?

avatar

Sorry, there wasn't any repo for this course, as it was multiple small examples in this course so I didn't feel the need to save them as repository/repositories.

avatar

Okay thank for your response

avatar
You can use Markdown
avatar

can i receive source codes related to polymophic explanations ???

avatar

Sorry when shooting this course I didn't save the source into repository

avatar

nowaday, many lectures provide the source code. for example udemy... so i tald you

avatar

Yes I do provide the source code for courses that have one project created throughout the course. But not for courses where separate lessons are with small experiments like this one.

avatar
You can use Markdown
avatar
You can use Markdown