Rizwanullah Mohib
Rizwanullah Mohib

Reputation: 1

How to display two models data in lavavel blade view selected by with method

I have selected data from two models by with() method and now I want to display these Two models' record in view, how can I do this.

$posts=Post::where('slug','=',Str::lower($id))->with('comment')->first();

Upvotes: 0

Views: 75

Answers (2)

R.tbr
R.tbr

Reputation: 310

In your controller use this

$posts=Post::where('slug','=',Str::lower($id))->get();
return view('view.name',compact('posts');

Lets try this in your blade

@foreach($posts as $val)
  {{$val->comment()->id}}
@endforeach

Upvotes: 1

Sobir
Sobir

Reputation: 177

In controller

return view('view.name', ['posts' => $posts]);   

In view

@foreach($posts as $post)
//do something
@endforeach

Upvotes: 0

Related Questions