Laravel Datatables搜索嵌套的Relationship

时间:2017-07-09 06:22:21

标签: laravel datatables

我目前正在使用数据标签7.0和Laravel 5.4 我的网格中的数据显示正确,但是当我想搜索嵌套关系列时,我得到以下错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'course_semester.semester.name' in 'where clause' (SQL: select count(*) as aggregate from (select '1' as `row_count` from `videos` where LOWER(`videos`.`id`) LIKE %%te%% and LOWER(`videos`.`name`) LIKE %%te%% and LOWER(`course_semester`.`semester`.`name`) LIKE %%te%%) count_row_table)

这是我的代码

控制器

$videos = Video::with(['course_semester' => function ($query) {
            return $query->with('course', 'semester');
        }])->select('videos.*');
        return Datatables::of($videos)
                ->addColumn('check', '<input type="checkbox" name="selected-videos" value="{{$id}}">')
                ->escapeColumns([])
                ->make(true);

的Javascript

columns: [
                 { data: 'check' , name: 'check',orderable: false, searchable: false },
                 { data: 'id', name: 'videos.id' },
                 { data: 'name', name: 'videos.name' },
                 { data: 'course_semester.semester.name', name: 'course_semester.semester.name'},
                 { data: 'course_semester.course.name', name: 'course_semester.course.name'},
                 { data: 'status', name: 'videos.status' },
                 { data: 'comment', name: 'videos.comment' },
                 { data: 'video_date', name: 'videos.video_date' },
            ]

任何人都可以帮助我并注意我的问题吗?

提前致谢。

4 个答案:

答案 0 :(得分:3)

1- 数据表函数

public function dataTable($query)
{
    return datatables()
        ->eloquent($query)
        ->addColumn('BulkSelection', function ($category) 
        {
        ->addColumn('company', function ($category) 
        {
            return $category->company ? $category->company->name : 'No Company';
        })
        ->rawColumns(['action', 'company', 'createdAt','BulkSelection']);
}

2-查询功能

    public function query(Category $model)
{
        return $model->with('company:id,name')->select('categories.*')->newQuery();
}

3- getColumns 函数

$getCols = [
     
        'id',
        'name',
        'company'=> [
            'data'=>'company',
            'name'=>'company.name'
        ],
        'createdAt' => [
            'name'=>'created_at',
            'searchable' => false
        ],
        'action' => [
            'exportable' => false,
            'searchable'=>false,
            'orderable' => false
        ]
    ];

    return $getCols;

答案 1 :(得分:2)

由于此处使用select(),因此您不会加载关系。另外,使用nested eager loading的点语法:

$videos = Video::with(['course_semester', 'course_semester.course'])

答案 2 :(得分:1)

 $getCols = [
           
         
            'title',
            'Images' => [
                'name'=>'images_count',
                'data'=>'images_count',
                'searchable'=>false
            ],
            'price' => ['name'=>'start_price', 'data'=>'start_price'],
            'reserve_price',
            'Category' => [
                'data'=>'show_category',
                'name'=>'categories.name',
                'searchable'=>false
            ],
            'created_by' => [
                'name'=>'user.name',
            ],
            'createdAt' => [
                'name'=>'created_at',
                'searchable' => false
            ],
            'action' => [
                'exportable' => false,
                'searchable'=>false,
                'orderable' => false
            ]

答案 3 :(得分:0)

如果您只需要从关系表中加载数据,请使用以下代码

$videos = Video::with('course_semester','course', 'semester')->select('videos.*');
        return Datatables::eloquent($videos)
                ->addColumn('check', function(Video $v){ 
                  return '<input type="checkbox" name="selected-videos" value="{{$v->id}}">'
                  })
                ->rawColumns(['check'])
                ->make(true);

在脚本中,您可以将关联值称为,

          {data: 'course_semester.id'},
          {data: 'course.name'},
          {data: 'semester.name'},