簡體   English   中英

Laravel 背包 (select2_multiple)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM