Last active
          May 31, 2024 09:03 
        
      - 
      
- 
        Save ctf0/874e276b8d30e961bb9f359eac956988 to your computer and use it in GitHub Desktop. 
Revisions
- 
        ctf0 revised this gist Dec 21, 2020 . 1 changed file with 12 additions and 12 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,17 +28,6 @@ class Taggable extends MorphPivot } ``` > under tag model ```php <?php @@ -67,7 +56,18 @@ class Tag extends Model } ``` > under the related model ```php <?php public function tags() { return $this->morphToMany(Tag::class, 'taggable') ->using(Taggable::class); } ``` > now use it like ```php Tag::first()->related_models; ``` 
- 
        ctf0 revised this gist Aug 8, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,7 +23,7 @@ class Taggable extends MorphPivot public function related() { return $this->morphTo(__FUNCTION__, 'taggable_type', 'taggable_id'); } } ``` 
- 
        ctf0 revised this gist Jul 30, 2020 . 1 changed file with 5 additions and 8 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ - [thanx to](https://stackoverflow.com/a/55491374/3574919) setup oneToMany relation between `tag & taggable` > under taggable model ```php <?php @@ -39,12 +39,14 @@ public function tags() } ``` > under tag model ```php <?php class Tag extends Model { protected $with = ['taggables.related']; /* -------------------------------------------------------------------------- */ /* RELATIONS */ /* -------------------------------------------------------------------------- */ @@ -60,12 +62,7 @@ class Tag extends Model public function getRelatedModelsAttribute() { return $this->taggables->groupBy('taggable_type'); } } ``` 
- 
        ctf0 revised this gist Jul 27, 2020 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,6 @@ use Illuminate\Database\Eloquent\Relations\MorphPivot; class Taggable extends MorphPivot { protected $table = 'taggables'; /* -------------------------------------------------------------------------- */ /* RELATIONS */ @@ -61,7 +60,7 @@ class Tag extends Model public function getRelatedModelsAttribute() { return $this->load(['taggables.related']) ->taggables ->groupBy('taggable_type') ->map(function ($v, $k) { 
- 
        ctf0 revised this gist Jul 27, 2020 . 1 changed file with 15 additions and 3 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,12 +6,13 @@ ```php <?php use Illuminate\Database\Eloquent\Relations\MorphPivot; class Taggable extends MorphPivot { protected $table = 'taggables'; protected $with = ['related']; /* -------------------------------------------------------------------------- */ /* RELATIONS */ /* -------------------------------------------------------------------------- */ @@ -28,6 +29,17 @@ class Taggable extends Model } ``` > under the related model ```php <?php public function tags() { return $this->morphToMany(Tag::class, 'taggable') ->using(Taggable::class); } ``` > under tag model add ```php <?php 
- 
        ctf0 revised this gist Jul 27, 2020 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ - follow https://laravel.com/docs/master/eloquent-relationships#many-to-many-polymorphic-relations except `Defining The Inverse Of The Relationship`, instead we will use the below - [thanx to](https://stackoverflow.com/a/55491374/3574919) setup oneToMany relation between `tag & taggable` > under taggable model add 
- 
        ctf0 revised this gist Jul 27, 2020 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -33,8 +33,6 @@ class Taggable extends Model class Tag extends Model { /* -------------------------------------------------------------------------- */ /* RELATIONS */ /* -------------------------------------------------------------------------- */ 
- 
        ctf0 revised this gist Jul 27, 2020 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,7 +22,7 @@ class Taggable extends Model public function related() { return $this->morphTo(__FUNCTION__, taggable_type', 'taggable_id'); } } ``` @@ -50,7 +50,8 @@ class Tag extends Model public function getRelatedModelsAttribute() { return $this->load('taggables) ->taggables ->groupBy('taggable_type') ->map(function ($v, $k) { return app($k)->whereIn('id', $v->pluck('taggable_id'))->get(); 
- 
        ctf0 revised this gist Jul 21, 2020 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -48,7 +48,7 @@ class Tag extends Model /* ACCESSORS */ /* -------------------------------------------------------------------------- */ public function getRelatedModelsAttribute() { return $this->taggables ->groupBy('taggable_type') @@ -61,5 +61,5 @@ class Tag extends Model > and now use it like ```php Tag::first()->related_models; ``` 
- 
        ctf0 revised this gist Jul 21, 2020 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -35,8 +35,6 @@ class Tag extends Model { protected $with = ['taggables']; /* -------------------------------------------------------------------------- */ /* RELATIONS */ /* -------------------------------------------------------------------------- */ 
- 
        ctf0 renamed this gist Jul 21, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewingFile renamed without changes.
- 
        ctf0 created this gist Jul 21, 2020 .There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,67 @@ - follow https://laravel.com/docs/master/eloquent-relationships#many-to-many-polymorphic-relations as usual - [thanx to](https://stackoverflow.com/a/55491374/3574919) setup oneToMany relation between `tag & taggable` > under taggable model add ```php <?php class Taggable extends Model { protected $with = ['related']; public $timestamps = false; /* -------------------------------------------------------------------------- */ /* RELATIONS */ /* -------------------------------------------------------------------------- */ public function tag() { return $this->belongsTo(Tag::class); } public function related() { return $this->morphTo(); } } ``` > under tag model add ```php <?php class Tag extends Model { protected $with = ['taggables']; public $timestamps = false; /* -------------------------------------------------------------------------- */ /* RELATIONS */ /* -------------------------------------------------------------------------- */ public function taggables() { return $this->hasMany(Taggable::class); } /* -------------------------------------------------------------------------- */ /* ACCESSORS */ /* -------------------------------------------------------------------------- */ public function getItemsAttribute() { return $this->taggables ->groupBy('taggable_type') ->map(function ($v, $k) { return app($k)->whereIn('id', $v->pluck('taggable_id'))->get(); })->all(); } } ``` > and now use it like ```php Tag::first()->items; ```