使用 laravel ducmunet 示例:
有像这样的表
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
And 3 model (post, command and video) .
Comment model has morphTo relation with post and video.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
/**
* Get all of the owning commentable models.
*/
public function commentable()
{
return …Run Code Online (Sandbox Code Playgroud)