Reputation: 81
i want view data with the specific id my post.
my controller is :
public function profile(){
$data = Admin::with(array(5,6));
return view('profile')->with('data',$data);
}
i want view post
in id
'5' :
<div class="content-about">
<p> {{ $data[5][post]}} </p>
</div>
and the other view i will post
in id
'6'
<div class="content-about">
<p> {{ $data[6][post]}} </p>
</div>
Upvotes: 2
Views: 16387
Reputation: 3407
Try this: Your data is an Array of object
Controller:
public function profile(){
$data = Admin::whereIn('id', [5,6])->get();
return view('profile')->with('data',$data);
}
In your view:
@foreach($data as $key => $post)
<div class="content-about">
<p> {{$post->id}} </p>
<p> {{$post->post}}</p>
</div>
@endforeach
Upvotes: 0
Reputation: 1074
First, your query is incorrect. Model::with([...])
is eager loading specified relations. But Admin::with(array(5,6))
passing numeric values to the function, they are ignored as with()
expects them to be a string naming your relations. So with(array(5,6))
actually does nothing, simply return the query instance.
To load your Admin
with primary key 5 and 6, you need to use whereIn()
.
$data = Admin::whereIn('id', array(5,6))->get();
Now you have the result data set. Then you want to access the result item by primary key. The result data set $data
is an instance of Illuminate\Database\Eloquent\Collection
which support such feature via keyBy()
.
You want to access item by primary key id
, so here is your code should look like.
$data = Admin::whereIn('id', array(5,6))->get()->keyBy('id');
Upvotes: 3