我想显示“项目”名称。在该项目下,我创建了一个事件,并且在列表页面上,我想显示该项目的名称而不是该项目的ID。
两个表共享它们之间的关系。 我正在使用laravel背包。 1)下面是事件模型。
public function eventProject()
{
return $this->belongsTo('App\Models\Project','project_id','id');
}
2)像这样的项目模型。
public function projectEvent()
{
return $this->hasMany('App\Models\Event', 'project_id', 'id');
}
如何在网格列表中显示“项目名称”,“事件名称”等?
答案 0 :(得分:0)
我希望这会有所帮助。 您将一个setColumnDetails放入crud控制器的setupListOperation中。
$this->crud->setColumnDetails( 'project_id', [
'label' => "Project", // Table column heading
'type' => 'text',
'name' => 'project.project_name', // the column that contains the ID of that connected entity;
// 'entity' => 'project', // the method that defines the relationship in your Model
'attribute' => "project.name", // foreign key attribute that is shown to user
'model' => "App\Models\Project", // foreign key model,
'orderable' => true,
'orderLogic' => function ( $query, $column, $column_direction ) {
return $query->join( 'projects', 'projects.id', '=', 'events.project_id' )
->orderBy( 'project.id', 'desc' );
},
'searchLogic' => function ( $query, $column, $searchTerm ) {
$query->orWhereHas( 'project', function ( $q ) use ( $column, $searchTerm ) {
$q->where( 'projects.name', 'like', '%' . $searchTerm . '%' );
} );
}
] );
这基本上只是将其从id更改为name字段,假设项目名称存储在“ name”字段中。