RomainB
3 months ago
164
3
Laravel

[email protected] with a call to $this->anotherAction() isn't working?

Posted 3 months ago by RomainB

Hi. To manage post on a blog website, I've got a high-level category named "Blog" displayed on /blog/, and some others categories displayed on root directory with the uri /category/{category}.

To display category's post I've got a CategoryController with show() method:

    public function show(Category $category)
    {
        $posts = $category->posts;
        return view('public.post.index')
                ->with('posts', $posts);
    }

And to display "blog" post, I've got a blog() method, which to DRY my code, call $this->show() method:


    public function blog()
    {
        $category = Category::findOrFail(1);
        $this->show($category);
    }

I've got the routes: /category/{category} to display all categories's post, and /blog/ to filter only the post where category_id = 1

Don't know if it's the better way to do this (I could probably simply filter /blog/ uri in my show() method) but this isn't the question.

In fact I'M just surprised to see that $this->show($category) in my blog() method, works (tested with dd()) but the return view isn't! Only a white page is rendered when /blog is requested

My posts table for now just have 4 rows, all for /blog/ so the root uri: /category/1 and /blog should be the same.

I tried to dd($posts) > in the show() method > The 4 rows are properly return in both ways (/blog and /category/1). So why the view() method silently screw up on /blog request?

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