Filter Eloquent model

Posted 3 years ago by matthijs110

I am trying to filter my Eloquent model, but I am a bit stuck now..

This is the function to get all topics:

/**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($category = null)
    {
        $categories = Category::all();

        $topics = Topic::latest()->with('category', 'user', 'replies')->paginate(5);

        return view('forum.index', compact('categories', 'topics'));
    }

What I try to do is, when a certain route has been request: /category/{category} I want the result to only show topics where() the slug equals to $category.

How would I do that? I doubt about my approach.

Please sign in or create an account to participate in this conversation.