Donations Appreciated

No one has ever become poor by giving. - Anne Frank, diary of Anne Frank

Click here to lend your support to: Laravel Datatables and make a donation at pledgie.com !

EloquentController.php - Join

Heads Up! When using join statements, you must follow the table.column approach on your js columns definition.

Example columns definition:

columns: [{data: 'id', name: 'posts.id'}, {data: 'name', name: 'users.name'}]

Id Title Author Name Created At Updated At

EloquentController.php

    public function getJoins()
    {
        return view('datatables.eloquent.joins');
    }

    public function getJoinsData()
    {
        $posts = Post::join('users', 'posts.user_id', '=', 'users.id')
            ->select(['posts.id', 'posts.title', 'users.name', 'users.email', 'posts.created_at', 'posts.updated_at']);

        return Datatables::of($posts)
            ->editColumn('title', '{!! str_limit($title, 60) !!}')
            ->editColumn('name', function ($model) {
                return \HTML::mailto($model->email, $model->name);
            })
            ->make(true);
    }

Javascript

    $('#posts-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: 'https://datatables.yajrabox.com/eloquent/joins-data',
        columns: [
            {data: 'id', name: 'posts.id'},
            {data: 'title', name: 'posts.title'},
            {data: 'name', name: 'users.name'},
            {data: 'created_at', name: 'posts.created_at'},
            {data: 'updated_at', name: 'posts.updated_at'}
        ]
    });