【问题标题】:How to display data with yajra datatable many to many relationship in laravel?如何在 laravel 中使用 yajra 数据表多对多关系显示数据?
【发布时间】:2020-09-17 04:43:58
【问题描述】:

我正在使用 Yajra 数据表。我想用总统姓名显示数据和过滤数据 我有 3 张桌子

1- Planes = id, title, description.

2- presidents = id, P_name.

3- Plane_president = plane_id , president_id

平面模型:

public function president()
    {
        return $this->belongsToMany(President::class);
    }

总裁模特:

public function planes()
{
    return $this->belongsToMany(Plane::class);
}

我的控制器:

public function index(Request $request)
    {
    if ($request->ajax()) {
            $query = Plane::with('presidents')->selectRaw('distinct planes.*');
            return $this->dataTable
                ->eloquent($query)
                ->addColumn('P_name', function (Plane $plane) {
                    return $plane->presidents->map(function($president) {
                        return str_limit($president->P_name);
                    })->implode('<br>');
                })
                ->make(true);
        }

        return view('planes.index');
    }

js代码:

  <script type="text/javascript">
      $('#search').DataTable({
        processing: true,
        serverSide: true,
        ajax: '{{ route("plane.index") }}',
        columns: [
            {data: 'id', name: 'id'},
            {data: 'main_point', name: 'main_point'},
            {data: 'presidents[, ].P_name', name: 'president'},
        ]
    });
    </script>

我有这个错误

message "Undefined property: App\\Http\\Controllers\\PlaneController::$dataTable"

如何解决这个错误?

【问题讨论】:

    标签: laravel yajra-datatable


    【解决方案1】:

    使用DataTables:: 制作datatable 而你缺少-&gt;rawColumns(['p_name']) 我补充说

    if ($request->ajax()) {
        $plane = Plane::with('presidents')->selectRaw('distinct planes.*')->get();
        return \DataTables::of($plane)
            ->addColumn('p_name', function ($plane) {
                return implode(', ', $plane->presidents->pluck('P_name')->toArray());
            })
            ->rawColumns(['p_name'])
            ->make(true);
    }
    

    在 JavaScript 中

    <script type="text/javascript">
          $('#search').DataTable({
            processing: true,
            serverSide: true,
            ajax: '{{ route("plane.index") }}',
            columns: [
                {data: 'id', name: 'id'},
                {data: 'main_point', name: 'main_point'},
                {data: 'p_name', name: 'p_name'}, // manipulate data of this column in server side here just echo like this
            ]
        });
    </script>
    

    【讨论】:

      【解决方案2】:

      你需要按如下方式传递。

      return datatables()->eloquent($query);
      

      请查看document。我建议显示总统姓名。

      implode(', ', $plane->presidents->pluck('P_name')->toArray())
      

      【讨论】:

      • 当我想搜索数据时出现此错误DataTables warning: table id=search - Exception Message: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'planes.presidents' in 'where clause' (SQL: select count(*) as aggregate from (select distinct planes.* from planes` where (LOWER(planes.id) LIKE %j% or LOWER(planes.main_point ) LIKE %j% 或 LOWER(planes.presidents) LIKE %j%)) count_row_table)`
      猜你喜欢
      • 2021-08-25
      • 2021-03-26
      • 2021-08-08
      • 2013-05-06
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多