Eloquent Polymorphic Many-to-Many: Practical Example

  Рет қаралды 10,456

Laravel Daily

Laravel Daily

Күн бұрын

If you have Tags that may belong to many Posts/Videos/Courses, how to search all records by a specific tag?
Text article version of this video: laraveldaily.com/post/laravel...
- - - - -
Support the channel by checking out my products:
- My Laravel courses: laraveldaily.com/courses
- Laravel QuickAdminPanel: quickadminpanel.com
- Livewire Kit Components: livewirekit.com
- - - - -
Other places to follow:
- My weekly Laravel newsletter: us11.campaign-archive.com/hom...
- My personal Twitter: / povilaskorop

Пікірлер: 7
@ronssijei
@ronssijei Жыл бұрын
Found a solution to this by creating a separate Taggable model. In your Taggable model you can add this. public function taggable(): MorphTo { return $this->morphTo(); } and in your Tag model you can add this. public function related(): MorphOne { return $this->morphOne(Taggable::class, 'taggable'); } and now your query would look like this. $tags = Taggable::with(['taggable'])->get(); $tags->map(function ($tag) { return [ 'id' => $tag->taggable->id, 'title' => $tag->taggable->title ]; });
@LaravelDaily
@LaravelDaily Жыл бұрын
Looks cool! Another possible solution, yes.
@UmbertoPanella
@UmbertoPanella Жыл бұрын
Hi. Instead of map to create same structure we can use appends on model (or with or other idea) also using eager loading method...(?) Will enjoy only on model specification.
@GergelyCsermely
@GergelyCsermely Жыл бұрын
Thanks
@florenciodev3867
@florenciodev3867 Жыл бұрын
I don't like to put logic on database. what about if I don't want to use laravel in future! I don't think that's professional. what's your opinion?
@user-hk3te3ql5p
@user-hk3te3ql5p Жыл бұрын
does quickadminpanel support this relationship?
@LaravelDaily
@LaravelDaily Жыл бұрын
No we don't support polymorphic relations, it would be too complex to generate for all possible cases.
Laravel Pivot Tables: Simple to Advanced Many-to-Many
12:24
Laravel Daily
Рет қаралды 117 М.
Laravel Eloquent: Deeper Relationships with One Query
10:37
Laravel Daily
Рет қаралды 135 М.
1🥺🎉 #thankyou
00:29
はじめしゃちょー(hajime)
Рет қаралды 77 МЛН
Bro be careful where you drop the ball  #learnfromkhaby  #comedy
00:19
Khaby. Lame
Рет қаралды 44 МЛН
File Upload in Laravel: Main Things You Need To Know
13:58
Laravel Daily
Рет қаралды 19 М.
Laravel Polymorphic Many-to-Many Performance: Query Pivot Table
5:54
Laravel TALL vs VILT Stack: Explained with Examples
12:38
Laravel Daily
Рет қаралды 10 М.
Laravel: Create Public API with Cache and Rate Limits
12:18
Laravel Daily
Рет қаралды 44 М.
Livewire or Vue.js: Which to Use When?
12:13
Laravel Daily
Рет қаралды 12 М.
Eloquent where(): 6 "Tricks" You May Not Know
5:44
Laravel Daily
Рет қаралды 9 М.
Wanna Start with Filament? Quick 5-Minute Demo.
7:50
Laravel Daily
Рет қаралды 6 М.
Laravel Roles and Permissions: All CORE Things You Need To Know
16:32
Laravel Daily
Рет қаралды 212 М.
Laravel UUID with Foreign Keys: Speed Benchmark of Two Approaches
5:49
Sigma girl have a good heart #shorts #tiktok #sigmagirl
0:37
mountainlion5
Рет қаралды 18 МЛН
MOM TURNED THE NOODLES PINK😱
0:31
JULI_PROETO
Рет қаралды 12 МЛН