How to automatically move data to the view in Laravel Resource Controller?

Asked

Viewed 808 times

2

I am using Laravel 5.6 with Resource Controller. My show method:

public function show(Artigo $artigo)
{
    $artigo = $artigo->first();
    return view('painel.revista.detalhes', compact('artigo'));
}

It would be possible to ignore the line $artigo = $artigo->first(); and pass the variable directly to the view? I tried:

public function show(Artigo $artigo)
{
    return view('painel.revista.detalhes', compact('artigo'));
}

However, I could not access the data in the view, I tried: {{ $artigo->titulo }} but it didn’t work.

Article Collection was included as a parameter when running:

php artisan make:controller ConteudoController -m Models/Artigo
  • You’re only doing the injection so in the body you have to call the First().

2 answers

0

public function show(Artigo $artigo)
{
    return view('painel.revista.detalhes', ['artigo' => $artigo->first()]);
}

Then I could access with {{ $artigo->titulo }}

  • Can I access the specific record passed as a parameter in the Resource route? For example: controller Resource in show call gets an ID, my Collection can access this record directly without having to do $article->find...?

0

Inside the controller before the

$article = $article->first();

of a DD, the correct one must be listing only one object and not the Collection.

In the listing view, you are passing only one object?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.