Laravel-datatables: orderBy colunm id desc

Created on 7 Apr 2016  ·  16Comments  ·  Source: yajra/laravel-datatables

Hi,
I want sort colum id desc ?
Plz help me
Thanks :)

Most helpful comment

Just use the order method on your javascript like below. Use the column index of id.

$('table').DataTable({
    ...
    order: [ [0, 'desc'] ]
})

All 16 comments

Just use the order method on your javascript like below. Use the column index of id.

$('table').DataTable({
    ...
    order: [ [0, 'desc'] ]
})

i want via sql eloquent. Cant u help me? :)

Can you give us an example of your code?

Sorting in Eloquent is done with ->orderBy('id','desc') or ->latest('id')

But when out outside table then i sorting column is not working

I believe using sorting in your eloquent overrides the sorting functionality in datatables.
At least, when I used it, sorting didn't work anymore.

But again, show us your code. We can help you better with an example of what you're trying to achieve.

thanks for the helper :)

@ArtbyArjan is correct that setting the order on your query manually will make the sorting not to work anymore. If you want to sort by ID by default, use the order that I suggested above. Else just click on ID header to trigger the sort functionality.

Yes @yajra and @ArtbyArjan you can use ->orderBy in Eloquent. However, by doing this will disable the ajax feature <th> class sorting. As tested by doing $policies = Policy::query()->latest() at PostDataTable.php. I think @phainv wanted to order by desc and likewise the <th> sorting feature.

If you are using as a service, in the method html(), on parameters array use

/**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\Datatables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
            ->columns($this->getColumns())
            ->parameters([
                'order' => [
                    0, // here is the column number
                    'desc'
                ]
           ]);

How do you sort with a column that you don't wish to display on the table ?
I don't wish to display IDs on the table. I also wish to orderBy updated_at and I don't wish to display updated_at on the table

Hi @digitlimit , you can force an order in the query param, like:

public function query()
{
        $myModel = Person::query()->orderBy('updated_at', 'DESC');

        return $this->applyScopes($myModel);
}

But you'll need to set the param orderable to false in the columns function.

If you are using as a service, in the method html(), on parameters array use

/**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\Datatables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
            ->columns($this->getColumns())
            ->parameters([
                'order' => [
                    0, // here is the column number
                    'desc'
                ]
           ]);

thanks bro its working

if you want to get code then plz reply me

@rafaelqm How about ordering raw column?

@rafaelqm How about ordering raw column?

@RayhanYulanda do you mean that you have a raw query? Because works the same way, just put the name you give to the alias of the column.

@rafaelqm How about ordering raw column?

@RayhanYulanda do you mean that you have a raw query? Because works the same way, just put the name you give to the alias of the column.

No, i mean raw column that generate button text. I need to search and order that column too

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ahmadbadpey picture ahmadbadpey  ·  3Comments

vipin733 picture vipin733  ·  3Comments

jgatringer picture jgatringer  ·  3Comments

techguydev picture techguydev  ·  3Comments

Mopster picture Mopster  ·  3Comments