Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在laravel背包中显示表中的多对多数据?_Laravel_Eloquent_Laravel Backpack - Fatal编程技术网

如何在laravel背包中显示表中的多对多数据?

如何在laravel背包中显示表中的多对多数据?,laravel,eloquent,laravel-backpack,Laravel,Eloquent,Laravel Backpack,我在用户和事件之间有一种多对多的关系。每个用户都可以参加许多活动,每个活动都可以有许多用户参加 我想使用Laravel双肩包在事件表中显示所有参加过事件的用户 我在事件和用户模型中分别添加了这两个实体之间的关系 public function users(){ return $this->belongsToMany('App\Models\User','user_event'); } 然后,我在EventCrudController的函数setUp

我在用户和事件之间有一种多对多的关系。每个用户都可以参加许多活动,每个活动都可以有许多用户参加

我想使用Laravel双肩包在事件表中显示所有参加过事件的用户

我在事件和用户模型中分别添加了这两个实体之间的关系

    public function users(){
            return $this->belongsToMany('App\Models\User','user_event');
    }
然后,我在EventCrudController的函数setUpListOperation中添加了列

最后一步是在事件模型内创建一个函数,以检索要在事件表中显示的用户名

public function userName() {
   return $this->user ? $this->user->first_name : $this;
}

我找到了一个解决这个问题的方法。我不知道这个解决方案有多最优。我将userName函数替换为以下内容:公共函数userName(){$users=$this->users;$names='';foreach($users as$user){$names=$names.$user->name.'';}返回$names;}
    protected function setupListOperation()
    {
        // TODO: remove setFromDb() and manually define Columns, maybe Filters
        $this->crud->addColumns(['name','users']);

        $this->crud->setColumnDetails('users', [
            'name' => "users",
            'label' => "Users", // Table column heading
            'type' => "model_function",
            'function_name' => 'userName' // the method in your Model
        ]);
    }
public function userName() {
   return $this->user ? $this->user->first_name : $this;
}