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

问题描述

我与用户和事件之间有很多关系。每个用户可以参加许多事件,每个事件可以由许多用户参加。

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

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

    public function users(){
            return $this->belongsToMany('App\Models\User','user_event');
    }
public function events() {
        return $this->belongsToMany('App\Models\Event','user_event');
    }

然后,我在 EventCrudController

函数 setUpListOperation 添加了列
    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;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)