0

I am using http://datatables.net/and https://github.com/yajra/laravel-datatables-oracle. When I try search I get a popup with an "Ajax error" I seemed to have narrowed it down to being an issue when ever there is a join in the query.

Here is method used for the Ajax request (removed fields for simplicity):

$leads = Lead::leftJoin('lead_status', 'leads.status', '=', 'lead_status.id')
 ->select('leads.id', 'leads.email', 'lead_status.status');

This works fine as I said it is only when I try search do I get the error, I don't get it when sorting or paging through results.

When there is no join in the query then I don't get any issues.

Been on this for a while now and not sure what to do..

2
  • Surely this has nothing to do with datatables - the problem is related to your database query. Can you debug the results and compare them? Commented Apr 30, 2015 at 8:51
  • What could be wrong with the query? Commented Apr 30, 2015 at 13:20

1 Answer 1

3

When using a join statement, you have to specify in js the fully qualified name of field. You should use table.column pattern. See example code below:

$('#posts-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: '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'}
        ]
    });

And on your controller

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);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Dear Sir @yajra when I put this line in my code it gives me the error. ->editColumn('name', function ($model) { return \HTML::mailto($model->email, $model->name); }).As I am new to datatables stuck over here. please help me.
may I ask you to have a look at a yajra dataTable related question here :stackoverflow.com/questions/53208578/… ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.