关联关系 ¶
一对一关系 ¶
users表和上面的posts表为一对一关联关系,通过posts.author_id字段关联,users表结构如下:
sql
users
id - integer
name - string
email - string
posts
id - integer
author_id - integer
content - text模型定义为:
php
class User extends Model
{
}
class Post extends Model
{
public function author()
{
return $this->belongsTo(User::class, 'author_id');
}
}那么可以用下面的方式显示post所属的用户的详细:
php
$show->author('作者信息', function ($author) {
$author->setResource('/admin/users');
$author->id();
$author->name();
$author->email();
});其中$author对象也是Show实例,同样可以使用上面的各种方法
注意:为了能够正常使用这个面板右上角的工具,必须用
setResource()方法设置用户资源的url访问路径
一对多或多对多关系 ¶
一对多或多对多关系的关联数据会以Model-grid的方式呈现,下面是简单的例子
posts表和评论表comments为一对多关系(一条post有多条comments),通过comments.post_id字段关联
sql
comments
id - integer
post_id - integer
content - string模型定义为:
php
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class);
}
}
class Comment extends Model
{
}那么评论的显示通过下面的代码实现:
php
$show->comments('评论', function ($comments) {
$comments->resource('/admin/comments');
$comments->id();
$comments->content()->limit(10);
$comments->filter(function ($filter) {
$filter->like('content');
});
});$comments是一个Elegant\Admin\Grid实例,详细的使用方法可以参考model-grid
注意:为了能够正常使用这个数据表格的功能,必须用
resource()方法设置comments资源的url访问路径
Laravel-Admin文档