R
R
RMate2019-12-05 18:25:28
Software design
RMate, 2019-12-05 18:25:28

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) {
        //
    }
  }

But something inside me tells me that this is not the right way. Can you please tell me how to organize such logic correctly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Grigory Vasilkov, 2019-12-05
@RMate

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;

V
vism, 2019-12-06
@vism

That's right, that's what morphs are for. It gives beauty to solutions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question