Custom Polymorphic Types in Laravel

382 Views Asked by At

I am working with polymorphic relationships and created a polymorphic tables with columns like

posts -:
   | id(integer) | title(string) | body(text) |

videos -: 
   | id(integer) | title(string) | url(text) |

comments -: 
   | id(integer) | title(string) | commentable_id(integer) | commentable_type(string) |

The default commentable_type would be either App\Models\Post or App\Models\Video, respectively.

I do not want to use these value for commentable_type want some custom values, found one approach with morphMap like -

use Illuminate\Database\Eloquent\Relations\Relation;

Relation::morphMap([
    'posts' => 'App\Models\Post',
    'videos' => 'App\Models\Video',
]);

Looking for some other solution like some flag or method which can used in Model to achieve some results

0

There are 0 best solutions below