Laravel 5.5 doesn't recognize a variable

Published 10 months ago by JokersMild85

So I'm working through the Laravel 5 Fundamentals series on episode 13 where I just created the Articles resource controller and added the Edit view... not exactly complicated stuff. However, I am receiving an error message saying that I have an undefined variable $article.

I most definitely defined the variable correctly and the code for the Edit function is basically identical to the Show function. There are no syntax errors, the function returns the articles.edit view fine but it just does not want to recognize the $article variable. Any suggestions what I am doing wrong?

tykus
tykus
10 months ago (658,320 XP)

Can you share your Controller method and view code?

JokersMild85

Never mind, I figured it out as I was posting my code. Thanks anyway. I'm sure I'll be posting more questions here in the near future.

Stratos

What was your error? So others in the future can see how you solved it.

JokersMild85

Originally the controller code looked like this when I was receiving the undefined variable error:

class ArticlesController extends Controller {

public function show($id)
{     
    $article = Article::findOrFail($id);
    
    return view('articles.show', compact('article'));
}

public function edit($id)
{
    $article = Article::findOrFail($id);
    
    return view('articles.edit');
}

}

Once I added the compact('article) code to the Edit function the same way I applied it to the Show function, it worked fine.

Obvious mistake once you figure out what is causing the problem.

Thanks again guys.

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