📜  laravel 6 orderby - PHP (1)

📅  最后修改于: 2023-12-03 15:02:34.300000             🧑  作者: Mango

Laravel 6 orderby

When working with data in Laravel, it's sometimes necessary to sort the data in a specific order. Laravel provides the orderBy method to easily sort data based on a specified column.

Usage

To use the orderBy method, first retrieve the data using the get method:

$users = DB::table('users')
            ->orderBy('name', 'asc')
            ->get();

In this example, the data is being retrieved from the users table and sorted by the name column in ascending order.

The orderBy method can also be used to sort by multiple columns:

$users = DB::table('users')
            ->orderBy('name', 'asc')
            ->orderBy('email', 'asc')
            ->get();

In this example, the data is first sorted by the name column and then by the email column, both in ascending order.

The second parameter of the orderBy method can be either asc or desc, to specify ascending or descending order respectively:

$users = DB::table('users')
            ->orderBy('name', 'desc')
            ->get();

In this example, the data is being sorted by the name column in descending order.

Conclusion

Sorting data in Laravel is easy with the orderBy method. By using the method, you can quickly and easily sort data in any order you require.