Answer the question
In order to leave comments, you need to log in
Is there a pattern for morph links?
Hello!
I have several entities, with a common feature - each of them can have a shortened link, with a set of its own capabilities.
I made a relationship between other entities and a table of shortened links through a Morph link.
Now, in the controller I have this code:
public function show(ShortLink $link)
{
$model = $link->shortable; // Получаем связанную с сокрашенной ссылкой модель
if ($model instanceof Offer) {
//
}
}
Answer the question
In order to leave comments, you need to log in
Morphs, I would say in themselves such a crutch, easy.
Usually in database theory there are one-to-many relationships, and each entity has its own table.
The morph in this case is needed rather than the entity in order to pull out the link only if "the type is equal to the entity".
With the same success, you can simply store links and have tables for linking links to entities separately
Links
Vidos
Pictures
LinksVidos
LinksPictures
morph in this case looks like this
Links
Vidos
Pictures
LinksThings (type: vidos / pictures)
(?) TypesThings (1. vidos, 2. pictures )
In the case of how to work with this:
Models for each table
Class-Command "ItemRecipientByReference"
-- get(link)
Class-Command "ItemRecipientByItidType"
-- get(id, type)
Link Controller
-- ItemRecipientByReference->get(link)
Image Controller
-- ItemRecipientByIdType->get(image, id)
Vidos Controller
-- ItemReceiverByIdItType->get(vidos, id)
$link = Links::with('image')->where('code', '123456myshortlink')->first();
return $link->image;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question