Created
January 19, 2021 10:53
-
-
Save gundamew/db626d67148e9dd69993a934e311cbb4 to your computer and use it in GitHub Desktop.
A many-to-many polymorphic relations example of Laravel.
This file contains 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 characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Media extends Model {} |
This file contains 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 characters
<?php | |
namespace App; | |
use App\PostHasMedia; | |
use Illuminate\Database\Eloquent\Model; | |
class Post extends Model | |
{ | |
public function media() | |
{ | |
return $this->morphToMany( | |
Media::class, | |
'model', | |
PostHasMedia::class, | |
'post_id', | |
'media_id' | |
); | |
} | |
} |
This file contains 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 characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Relations\MorphPivot; | |
// Ref: https://github.com/laravel/framework/issues/21065#issuecomment-327836778 | |
class PostHasMedia extends MorphPivot {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment