[英]Backpack for Laravel (select2_multiple)
我需要 select 多个用户并将名称显示为开发人员。 但我得到这个错误。
此集合实例上不存在异常属性 [名称]。 (查看:D:\Laravel\BoomTech\bug-tracker\resources\views\project_issues.blade.php)
应显示用户姓名的代码:
{{ $issue->developer->name }}
问题表:
public function up()
{
Schema::create('issues', function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignId('project_id');
$table->foreignId('submitted_by_id');
$table->foreignId('developer_id');
$table->string('title', 255);
$table->string('comment', 255)->nullable();
$table->mediumText('description');
$table->text('status');
$table->text('type');
$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->nullable();
$table->timestamp('deleted_at')->nullable();
});
}
问题用户(数据透视表):
Schema::create('issue_user', function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignId('user_id');
$table->foreignId('issue_id')->nullable();
$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->nullable();
});
问题 Model:
public function developer()
{
return $this->belongsToMany('App\Models\User', 'issue_user', 'issue_id', 'user_id');
}
问题CRUD控制器:
CRUD::addField([ // Select2Multiple = n-n relationship (with pivot table)
'label' => "Developer",
'type' => 'select2_multiple',
'name' => 'developer', // the method that defines the relationship in your Model
// optional
'entity' => 'developer', // the method that defines the relationship in your Model
'model' => "App\Models\User", // foreign key model
'attribute' => 'name', // foreign key attribute that is shown to user
'pivot' => false, // on create&update, do you need to add/delete pivot table entries?
]);
这是多对多的关系,所以你需要这样做:
public function developers()
{
return $this->belongsToMany('App\Models\User', 'issue_user', 'issue_id', 'user_id');
}
@foreach($issue->developers() as $developer)
{{ $developer->name }}
@endforeach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.