在文档中找不到这个。
有没有标准方法,在不创建自定义小部件或覆盖视图模板的情况下,在 CRUD 的节目中显示多对多关系在 Backpack for Laravel 中操作?如果答案是否定的,那么您的实现方法是什么?
假设我有一个课程模型和一个用户模型,两者之间有一个多对多
class Course extends Model
{
public function students()
{
return $this->belongsToMany(User::class, 'course_students');
}
}
class User extends Model
{
public function courses()
{
return $this->belongsToMany(Course::class, 'course_students');
}
}
在课程的显示操作中。如何显示包含所有学生的表格?
事实上,您可以使用关系列来实现这一点
摘录:
输出相关条目,无论关系如何:
1-n 关系 - 输出其一个连接实体的名称;
n-n 关系 - 枚举其所有连接实体的名称;
其名称和定义与关系字段的名称和定义相同 类型:
[
// any type of relationship
'name' => 'tags', // name of relationship method in the model
'type' => 'relationship',
'label' => 'Tags', // Table column heading
// OPTIONAL
// 'entity' => 'tags', // the method that defines the relationship in your Model
// 'attribute' => 'name', // foreign key attribute that is shown to user
// 'model' => AppModelsCategory::class, // foreign key model
],
背包尝试猜测要为相关项目显示哪个属性。 最终用户会认为是独一无二的。如果是 像"名称"或"标题"这样常见的东西它会猜到它。如果没有,您 可以在列定义中手动指定属性,或者 您可以添加公共$identifiableAttribute = 'column_name';到您的 模型,背包将使用该列作为用户找到的列 识别。它将在这里使用它,它将在你任何地方使用它 没有明确要求其他属性。