首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel背包(select2_multiple)

Laravel背包(select2_multiple)
EN

Stack Overflow用户
提问于 2021-12-09 17:14:28
回答 1查看 125关注 0票数 0

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

此集合实例上不存在

异常属性名称。(浏览: D:\Laravel\BoomTech\bug-tracker\resources\views\project_issues.blade.php)

应该显示用户名称/s的代码:

代码语言:javascript
复制
{{ $issue->developer->name }}

问题表:

代码语言:javascript
复制
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();
    });
}

发布用户(枢轴表):

代码语言:javascript
复制
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();
    });

发行模式:

代码语言:javascript
复制
public function developer()
{
    return $this->belongsToMany('App\Models\User', 'issue_user', 'issue_id', 'user_id');
}

IssueCRUDController:

代码语言:javascript
复制
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?
    ]);
EN

回答 1

Stack Overflow用户

发布于 2021-12-09 17:32:58

这是很多关系,所以你需要这样做:

代码语言:javascript
复制
public function developers()
{
    return $this->belongsToMany('App\Models\User', 'issue_user', 'issue_id', 'user_id');
}


@foreach($issue->developers() as $developer)
{{ $developer->name }}
@endforeach
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70294000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档