Backpack for Laravel (select2_multiple)



我需要选择多个用户,并将名称显示为开发人员。但我犯了这个错误。

异常此集合实例上不存在属性[名称]。(视图: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();
});

发行型号:

public function developer()
{
return $this->belongsToMany('AppModelsUser', 'issue_user', 'issue_id', 'user_id');
}

IssueCRUD控制器:

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' => "AppModelsUser", // 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('AppModelsUser', 'issue_user', 'issue_id', 'user_id');
}

@foreach($issue->developers() as $developer)
{{ $developer->name }}
@endforeach

最新更新