Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ctf0/874e276b8d30e961bb9f359eac956988 to your computer and use it in GitHub Desktop.
Save ctf0/874e276b8d30e961bb9f359eac956988 to your computer and use it in GitHub Desktop.

Revisions

  1. ctf0 revised this gist Dec 21, 2020. 1 changed file with 12 additions and 12 deletions.
    24 changes: 12 additions & 12 deletions laravel auto resolve polymorphic many to many.md
    Original file line number Diff line number Diff line change
    @@ -28,17 +28,6 @@ class Taggable extends MorphPivot
    }
    ```

    > under the related model
    ```php
    <?php

    public function tags()
    {
    return $this->morphToMany(Tag::class, 'taggable')
    ->using(Taggable::class);
    }
    ```

    > under tag model
    ```php
    <?php
    @@ -67,7 +56,18 @@ class Tag extends Model
    }
    ```

    > and now use it like
    > 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;
    ```
  2. ctf0 revised this gist Aug 8, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion laravel auto resolve polymorphic many to many.md
    Original 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');
    return $this->morphTo(__FUNCTION__, 'taggable_type', 'taggable_id');
    }
    }
    ```
  3. ctf0 revised this gist Jul 30, 2020. 1 changed file with 5 additions and 8 deletions.
    13 changes: 5 additions & 8 deletions laravel auto resolve polymorphic many to many.md
    Original 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 add
    > under taggable model
    ```php
    <?php

    @@ -39,12 +39,14 @@ public function tags()
    }
    ```

    > under tag model add
    > 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->load(['taggables.related'])
    ->taggables
    ->groupBy('taggable_type')
    ->map(function ($v, $k) {
    return app($k)->whereIn('id', $v->pluck('taggable_id'))->get();
    })->all();
    return $this->taggables->groupBy('taggable_type');
    }
    }
    ```
  4. ctf0 revised this gist Jul 27, 2020. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions laravel auto resolve polymorphic many to many.md
    Original 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';
    protected $with = ['related'];

    /* -------------------------------------------------------------------------- */
    /* RELATIONS */
    @@ -61,7 +60,7 @@ class Tag extends Model

    public function getRelatedModelsAttribute()
    {
    return $this->load('taggables)
    return $this->load(['taggables.related'])
    ->taggables
    ->groupBy('taggable_type')
    ->map(function ($v, $k) {
  5. ctf0 revised this gist Jul 27, 2020. 1 changed file with 15 additions and 3 deletions.
    18 changes: 15 additions & 3 deletions laravel auto resolve polymorphic many to many.md
    Original file line number Diff line number Diff line change
    @@ -6,12 +6,13 @@
    ```php
    <?php

    class Taggable extends Model
    use Illuminate\Database\Eloquent\Relations\MorphPivot;

    class Taggable extends MorphPivot
    {
    protected $table = 'taggables';
    protected $with = ['related'];

    public $timestamps = false;

    /* -------------------------------------------------------------------------- */
    /* 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
  6. ctf0 revised this gist Jul 27, 2020. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion laravel auto resolve polymorphic many to many.md
    Original 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 as usual
    - 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
  7. ctf0 revised this gist Jul 27, 2020. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions laravel auto resolve polymorphic many to many.md
    Original file line number Diff line number Diff line change
    @@ -33,8 +33,6 @@ class Taggable extends Model

    class Tag extends Model
    {
    protected $with = ['taggables'];

    /* -------------------------------------------------------------------------- */
    /* RELATIONS */
    /* -------------------------------------------------------------------------- */
  8. ctf0 revised this gist Jul 27, 2020. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions laravel auto resolve polymorphic many to many.md
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ class Taggable extends Model

    public function related()
    {
    return $this->morphTo();
    return $this->morphTo(__FUNCTION__, taggable_type', 'taggable_id');
    }
    }
    ```
    @@ -50,7 +50,8 @@ class Tag extends Model

    public function getRelatedModelsAttribute()
    {
    return $this->taggables
    return $this->load('taggables)
    ->taggables
    ->groupBy('taggable_type')
    ->map(function ($v, $k) {
    return app($k)->whereIn('id', $v->pluck('taggable_id'))->get();
  9. ctf0 revised this gist Jul 21, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions laravel auto resolve polymorphic many to many.md
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,7 @@ class Tag extends Model
    /* ACCESSORS */
    /* -------------------------------------------------------------------------- */

    public function getItemsAttribute()
    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()->items;
    Tag::first()->related_models;
    ```
  10. ctf0 revised this gist Jul 21, 2020. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions laravel auto resolve polymorphic many to many.md
    Original file line number Diff line number Diff line change
    @@ -35,8 +35,6 @@ class Tag extends Model
    {
    protected $with = ['taggables'];

    public $timestamps = false;

    /* -------------------------------------------------------------------------- */
    /* RELATIONS */
    /* -------------------------------------------------------------------------- */
  11. ctf0 renamed this gist Jul 21, 2020. 1 changed file with 0 additions and 0 deletions.
  12. ctf0 created this gist Jul 21, 2020.
    67 changes: 67 additions & 0 deletions setup.md
    Original 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;
    ```