Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AsnCode's avatar

orderBy created_at dont work

Hello i have some sorting in my app (inertia/vue).

My sortByPrice working Good My sortByCategory working Good But when i want to sort by Date thats dont work ....

Capsule.php


protected $dates = ['created_at'];

public function scopeSortByPrice($query, $sort)
    {
        return match ($sort) {
            'asc'    => $query->orderBy('price', 'asc') ?? null,
            'desc'   => $query->orderBy('price', 'desc') ?? null,
            default  => $query->orderBy('price', 'desc') ?? null,
        };
    }

    public function scopeSortByDate(Builder $query, $sort)
    {
        return match ($sort) {
            'asc'    => $query->orderBy('created_at', 'asc') ?? null,
            'desc'   => $query->orderBy('created_at', 'desc') ?? null,
            default  => $query->orderBy('created_at', 'desc') ?? null,
        };
    }

In my controller:

$capsules = Capsule::with(
            [
                'user:id,name,slug,username,profile_photo_path',
                'category:id,name,slug'
            ])
            ->where('approved', true)
            ->filter(request(['search', 'user_id', 'category_id', 'situation', 'gender', 'price_from', 'price_to', 'price_sort']))
            ->withCount(['likes as liked' => function (Builder $query) {
                $query->where('user_id', Auth::id());
            }, 'likes'])
            ->sortByPrice(request('sort_price'))
            ->sortByDate(request('sort_date'))
            ->latest()
            ->paginate(12)
            ->withQueryString();

in my Capsules/Index.vue

<div class="flex space-x-2">
                <select
                    @change="search"
                    id="sort_price"
                    v-model="form.sort_price"
                    class="mt-2 w-full md:w-40 block rounded-md border-0 mb-1 py-2 pl-3 pr-10 text-gray-500 ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-indigo-600 sm:text-sm sm:leading-6">
                    <option disabled :value="null">Price</option>
                    <option value="asc">croissant</option>
                    <option value="desc">decroissant</option>
                </select>

                <select
                    @change="search"
                    id="sort_date"
                    v-model="form.sort_date"
                    class="mt-2 w-full md:w-40 block rounded-md border-0 mb-1 py-2 pl-3 pr-10 text-gray-500 ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-indigo-600 sm:text-sm sm:leading-6">
                    <option disabled :value="null">Date</option>
                    <option value="desc">newer</option>
                    <option value="asc">older</option>
                </select>
            </div>
0 likes
4 replies
Snapey's avatar

->latest() will override any sort applied

Snapey's avatar

You are also calling both sortByPrice and sortByDate so both will be applied. If the sort is empty, you will apply the default case.

AsnCode's avatar

thanks @snapey Did Laravel have a system that i can appeal a scope when a request value exist ? or i need to make if/else in my controller ?

AsnCode's avatar

Ok i found just by refactoring my ->filter function thanks @snapey to guide me in the good path ;) Thats why i like laracasts ;) ++

Please or to participate in this conversation.