A
A
Anton2020-10-19 05:24:34
Laravel
Anton, 2020-10-19 05:24:34

How to display a separate news by id?

There is a NewsItemController responsible for displaying news by id:

class NewsItemController extends Controller
{
    public function show($id){
      $items = News::findOrFail($id);
      return view('item',compact('items'));
    }
}

Route :
Route::get("item/{id}", "[email protected]")->name('item');
News output:
<table>
        @foreach($items as $item)
            <tr>
                <td>{{$item->created_at}}</td>
                <td>{{$item->title}}</td>
                <td><a href="{{route('item',$item->id)}}">{{$item->title}}</a></td>
                <td>{{$item->anons}}</td>
            </tr>
        @endforeach
    </table>

Output of a single news when clicking on the title:
@foreach($items as $item)
        <tr>
            <td>{{ $item['title']}}</td>    
        </tr>
    @endforeach

Outputs nothing, but dd($items) outputs:
5f8cf80bd0e48065815081.png
I've been studying laravel for a couple of days and therefore I don't know where to look for the problem, I would appreciate a hint, even a superficial answer.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LaraLover, 2020-10-19
@Miron9900

You show yourself what you got. Why do you need a cycle? FindORFail returns a record, not a collection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question