2

I use laravel/datatables to list data.

I manipulated my datalist with addColumn function. However I am not able to use search in view for added columns. Because search is working according to database rows. Datatables works fine but in the view search area is not working. Because there isnt any fullname row in database table.

Here you can find my codes (I want to search for fullname but I can't)

Order Model

public function getFullname()
{
    return json_decode($this->getAttribute('delivery_adress'))->fullname;
}

OrderController.php

if ($request->ajax()) {
    return datatables()->of(Order::query())
        ->addColumn('fullname', function (Order $order) {
            return $order->getFullname();
        })->addColumn('city', function (Order $order) {
            return $order->getCity();
        })->addColumn('product_id', function (Order $order) {
            return $order->product->title;
        })->make(true);
}

Orders Blade / Datatables code

        "pageLength": 10,
        processing: true,
        serverSide: true,
        ajax: 'orders',
        columns: [
            { data: 'id', name: 'id'},
            { data: 'fullname', name: 'fullname', defaultContent: '-' , orderable: false },
            { data: 'city', name: 'city', defaultContent: '-', orderable: false  },
            { data: 'product_id', name: 'product_id', className: 'd-none d-sm-table-cell', defaultContent: '-' },
            { data: 'totalPrice', name: 'totalPrice', className: 'd-none d-sm-table-cell', defaultContent: '-' },
        ],

enter image description here

edit:

Finally, I found solution

{data: 'added_column', name: 'actual_column_name'}

https://github.com/yajra/laravel-datatables/issues/139#issuecomment-275326787

2
  • Please post your function in order model where to get getFullname() Commented Jan 28, 2020 at 7:25
  • I added my model. Commented Jan 28, 2020 at 7:51

3 Answers 3

1

Finally, I found solution

{data: 'added_column', name: 'actual_column_name'}

https://github.com/yajra/laravel-datatables/issues/139#issuecomment-275326787

Sign up to request clarification or add additional context in comments.

1 Comment

Nice share, works like a charm. :)
1

change status

serverSide : false

 "pageLength": 10,
        processing: true,
        serverSide: false,
        ajax: 'orders',
        columns: [
            { data: 'id', name: 'id'},
            { data: 'fullname', name: 'fullname', defaultContent: '-' , orderable: false },
            { data: 'city', name: 'city', defaultContent: '-', orderable: false  },
            { data: 'product_id', name: 'product_id', className: 'd-none d-sm-table-cell', defaultContent: '-' },
            { data: 'totalPrice', name: 'totalPrice', className: 'd-none d-sm-table-cell', defaultContent: '-' },
        ],

Comments

-1

Make sure to have relationship into Order model like this:

public function user()
{
    return $this->belongsTo('App\User');
}

Try to update like this :

...
    ->addColumn('fullname', function (Order $order) {
           return $order->user->fullname;
     })
...

1 Comment

I think you didnt understand my question. I dont have any problem with to list data. There is no problem. I just want to search in row which I added with addColumn function.

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.