I'm new to laravel and I tried to clear the problem from here
I have controller like the following:
foreach($users as $user){
$message[] = Model::where('id',$user->id)->get();
}
$data['tests'] = $message;
return View::make('user.index', $data);
My View is:
@foreach($tests['message'] as $test)
id : {{$test->id}}
@endforeach
which gives me Undefined index: message
I had dump the the $data
in the controller. my array is as shown below. I place the var_dump in the controller before the return statement. my var_dump($data)
is showing:
array(1) {
["tests"] => array(2) {
[0] => object(Illuminate\ Database\ Eloquent\ Collection) #515 (1) { ["items":protected]= > array(2) {
[0] => ["attributes": protected] => array(14) {
["id"] => string(3) "12"....
}
}
[1] => object(Illuminate\ Database\ Eloquent\ Collection) #515 (1) { ["items":protected]= > array(2) {
[0] => ["attributes": protected] => array(14) {
["id"] => string(3) "12"....
}
}
}
what i'm doing wrong. please help me
@foreach($tests as $test)
//$test is an array returned by get query.
@foreach($test as $item)
id : {{$item->id}}
@endforeach
@endforeach
get return array, if you want to return one element, use find() or first().
Your $tests['message']
should be $tests
because you have not define message in your controller.
$message = array();
foreach($users as $user){
$message[] = Model::where('id',$user->id)->get();
}
$data['tests'] = $message;
return View::make('user.index', $data);
View
@foreach($tests as $test)
id : {{$test->id}}
@endforeach
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With