1

query scope is not working, i'm doing dd() but can't solve the issue this way

queryscope method in model

public function scopeFilter($query, array $filters)
    {
        $query->when($filters['search'] ?? false, fn($query, $search) => $query
            ->where('title', 'like', '%' . $search . '%')
            ->orWhere('body', 'like', '%' . $search . '%')
        );

        $query->when($filters['category'] ?? false, fn($query, $category) => $query
            ->whereExists(fn($query) => $query
                ->from('categories')
                ->whereColumn('categories.id', 'posts.category_id')
                ->where('category.slug', $category)
            )
        );
    }

index (show all posts with filter) method in controller

public function index()
    {
        return view('posts', [
            'posts' => Post::latest()->filter(request(['search', 'category']))->get(),
            'categories' => Category::all()
        ]);
    }

when i do like this 127.0.0.1:8000/?categories=nihil-est-odit-nam-provident-omnis, it has to work and filter the posts with categories with given slug, but it is showing all the posts, even in the clockwork browser extension, it also showing the categories' => Category::all() line, and this is executed, not the actual filtered query

5
  • @Garry, should i write return two times? Commented Jan 9, 2022 at 13:08
  • You need only one return, at the end of scopeFilter, to return the query builder.
    – Alex
    Commented Jan 9, 2022 at 13:11
  • @Alex Scopes should always return the same query builder instance or void this is from documentation, return isn't necessary.
    – SiZE
    Commented Jan 9, 2022 at 13:36
  • @SiZE I stand corrected, you're right.
    – Alex
    Commented Jan 9, 2022 at 14:31
  • $query->when($filters['category'] ?? false, fn($query, $category) => $query ->whereHas('category', fn($query) => $query->where('slug', $category))); i did this, but still not working, what might be the issue in there? Commented Jan 10, 2022 at 5:58

1 Answer 1

1
public function scopeFilter($query, array $filters)
{
    $search = $filters['search'] ?? false;
    $category = $filters['category'] ?? false;

    $query
        ->when($search, function ($query, $search) {
            $query
                ->where('title', 'like', '%' . $search . '%')
                ->orWhere('body', 'like', '%' . $search . '%');
        })
        ->when($category, function ($query, $category) {
            $query
                ->whereHas('categories', function ($query) use ($category) {
                    $query->where('slug', $category);
                });
       });
 }

Assuming you have a categories() relationship defined in Post Model, I believe this should work for you.

Also dd(request(['search', 'category'])) to check if you are getting the array you want.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.