How to pass data to the view automatically on the Laravel Resource Controller?

2

I'm 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'));
}

Would it 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 was unable to access the data in the view, I tried: {{ $artigo->titulo }} but it did not work.

A collection Item was included as a parameter when running:

php artisan make:controller ConteudoController -m Models/Artigo
    
asked by anonymous 20.03.2018 / 21:26

2 answers

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

There you could access it with {{ $artigo->titulo }}

    
22.03.2018 / 00:29
0

Inside the controller before

  

$ article = $ article-> first ();

From a DD, the correct one should be listing only one object, not the collection.

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

    
25.03.2018 / 17:33