m2j m2j - 17 days ago 6
PHP Question

passing nested array from controller to view in laravel

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] => object(App\ Models\ MailSupport) #516 (24) { ["table":protected]= > string(17)
"create_mail_table" ["connection": protected] => NULL["primaryKey": protected] => string(2)
"id" ["keyType": protected] => string(3)
"int" ["perPage": protected] => int(15)["incrementing"] => bool(true)["timestamps"] => bool(true)["attributes": protected] => array(14) {
["id"] => string(3)
"198" ["message_id"] => string(16)
"15886a6f31731f0b" ["thread_id"] => string(16)
"15886a6f31731f0b" ["history_id"] => string(5)
"28228" ["label"] => string(5)
"INBOX" ["subject"] => string(14)
"first messsage" ["from_mail"] => string(36)
"Prakash K " ["to_mail"] => string(33)
"test oodoo " ["snippet"] => string(0)
"" ["body"] => string(4)
"hi " ["attachment"] => string(0)
"" ["time"] => string(19)
"2016-11-21 11:30:36" ["created_at"] => string(19)
"2016-11-21 11:30:42" ["updated_at"] => string(19)
"2016-11-21 11:30:42"
}["original": protected] => array(14) {
["id"] => string(3)
"198" ["message_id"] => string(16)
"15886a6f31731f0b" ["thread_id"] => string(16)
"15886a6f31731f0b" ["history_id"] => string(5)
"28228" ["label"] => string(5)
"INBOX" ["subject"] => string(14)
"first messsage" ["from_mail"] => string(36)
"Prakash K " ["to_mail"] => string(33)
"test oodoo " ["snippet"] => string(0)
"" ["body"] => string(4)
"hi " ["attachment"] => string(0)
"" ["time"] => string(19)
"2016-11-21 11:30:36" ["created_at"] => string(19)
"2016-11-21 11:30:42" ["updated_at"] => string(19)
"2016-11-21 11:30:42"
}["relations": protected] => array(0) {}["hidden": protected] => array(0) {}["visible": protected] => array(0) {}["appends": protected] => array(0) {}["fillable": protected] => array(0) {}["guarded": protected] => array(1) {
[0] => string(1)
"*"
}["dates": protected] => array(0) {}["dateFormat": protected] => NULL["casts": protected] => array(0) {}["touches": protected] => array(0) {}["observables": protected] => array(0) {}["with": protected] => array(0) {}["morphClass": protected] => NULL["exists"] => bool(true)["wasRecentlyCreated"] => bool(false)
}[1] => object(App\ Models\ MailSupport) #517 (24) { ["table":protected]= > string(17)
"create_mail_table" ["connection": protected] => NULL["primaryKey": protected] => string(2)
"id" ["keyType": protected] => string(3)
"int" ["perPage": protected] => int(15)["incrementing"] => bool(true)["timestamps"] => bool(true)["attributes": protected] => array(14) {
["id"] => string(3)
"199" ["message_id"] => string(16)
"15886a8343baef71" ["thread_id"] => string(16)
"15886a6f31731f0b" ["history_id"] => string(5)
"28364" ["label"] => string(4)
"SENT" ["subject"] => string(18)
"Re: first messsage" ["from_mail"] => string(33)
"test oodoo " ["to_mail"] => string(36)
"Prakash K " ["snippet"] => string(0)
"" ["body"] => string(95)
"reply On 21 November 2016 at 17:00, Prakash K wrote: > hi > " ["attachment"] => string(0)
"" ["time"] => string(19)
"2016-11-21 11:32:00" ["created_at"] => string(19)
"2016-11-21 11:32:08" ["updated_at"] => string(19)
"2016-11-21 11:32:08"
}["original": protected] => array(14) {
["id"] => string(3)
"199" ["message_id"] => string(16)
"15886a8343baef71" ["thread_id"] => string(16)
"15886a6f31731f0b" ["history_id"] => string(5)
"28364" ["label"] => string(4)
"SENT" ["subject"] => string(18)
"Re: first messsage" ["from_mail"] => string(33)
"test oodoo " ["to_mail"] => string(36)
"Prakash K " ["snippet"] => string(0)
"" ["body"] => string(95)
"reply On 21 November 2016 at 17:00, Prakash K wrote: > hi > " ["attachment"] => string(0)
"" ["time"] => string(19)
"2016-11-21 11:32:00" ["created_at"] => string(19)
"2016-11-21 11:32:08" ["updated_at"] => string(19)
"2016-11-21 11:32:08"
}["relations": protected] => array(0) {}["hidden": protected] => array(0) {}["visible": protected] => array(0) {}["appends": protected] => array(0) {}["fillable": protected] => array(0) {}["guarded": protected] => array(1) {
[0] => string(1)
"*"
}["dates": protected] => array(0) {}["dateFormat": protected] => NULL["casts": protected] => array(0) {}["touches": protected] => array(0) {}["observables": protected] => array(0) {}["with": protected] => array(0) {}["morphClass": protected] => NULL["exists"] => bool(true)["wasRecentlyCreated"] => bool(false)
}
}
}[1] => object(Illuminate\ Database\ Eloquent\ Collection) #507 (1) { ["items":protected]= > array(1) {
[0] => object(App\ Models\ MailSupport) #518 (24) { ["table":protected]= > string(17)
"create_mail_table" ["connection": protected] => NULL["primaryKey": protected] => string(2)
"id" ["keyType": protected] => string(3)
"int" ["perPage": protected] => int(15)["incrementing"] => bool(true)["timestamps"] => bool(true)["attributes": protected] => array(14) {
["id"] => string(3)
"201" ["message_id"] => string(16)
"15886ad2f39fe909" ["thread_id"] => string(16)
"15886ad2f39fe909" ["history_id"] => string(5)
"28368" ["label"] => string(5)
"INBOX" ["subject"] => string(15)
"with attachment" ["from_mail"] => string(36)
"Prakash K " ["to_mail"] => string(33)
"test oodoo " ["snippet"] => string(0)
"" ["body"] => string(12)
"image test " ["attachment"] => string(462)
"[{"
filename ":"
hqdefault.jpg ","
attachmentId ":"
ANGjdJ9n2BG1VOSyY2qk7yT - uYB - YPAqIgtw - 5 ALoPNIndlCVSQNTcxSEIVXdcShyslhkz35withhJ4HbXcRpB1pXUlXx4beUBSzyOpQufn4O99G4dn3sWS0hrvK0bo9rJTLUTYdvDH1Run6zJZIZCuQuiThNu0GwaDepksBgpCoJ1mXD73puBiW2dCmcN1jcx6RGsSp2dtC8tp0m - gDMzm3Id4Di9OT8QQRwwnYFZ5fxk - s_me85sa5D2uNW254U_ZnAOymfy3vx - n9BQMClIGTfXFHvS9dcKhyVlZFwoAA_kXsM8t - UFxGgrILQBA ","
filelocation ":"\ / home\ / prakash\ / woodoo\ / public\ / assets\ / dist\ / support\ / attach\ / hqdefault.jpg "}]" ["time"] => string(19)
"2016-11-21 11:37:25" ["created_at"] => string(19)
"2016-11-21 11:38:11" ["updated_at"] => string(19)
"2016-11-21 11:38:11"
}["original": protected] => array(14) {
["id"] => string(3)
"201" ["message_id"] => string(16)
"15886ad2f39fe909" ["thread_id"] => string(16)
"15886ad2f39fe909" ["history_id"] => string(5)
"28368" ["label"] => string(5)
"INBOX" ["subject"] => string(15)
"with attachment" ["from_mail"] => string(36)
"Prakash K " ["to_mail"] => string(33)
"test oodoo " ["snippet"] => string(0)
"" ["body"] => string(12)
"image test " ["attachment"] => string(462)
"[{"
filename ":"
hqdefault.jpg ","
attachmentId ":"
ANGjdJ9n2BG1VOSyY2qk7yT - uYB - YPAqIgtw - 5 ALoPNIndlCVSQNTcxSEIVXdcShyslhkz35withhJ4HbXcRpB1pXUlXx4beUBSzyOpQufn4O99G4dn3sWS0hrvK0bo9rJTLUTYdvDH1Run6zJZIZCuQuiThNu0GwaDepksBgpCoJ1mXD73puBiW2dCmcN1jcx6RGsSp2dtC8tp0m - gDMzm3Id4Di9OT8QQRwwnYFZ5fxk - s_me85sa5D2uNW254U_ZnAOymfy3vx - n9BQMClIGTfXFHvS9dcKhyVlZFwoAA_kXsM8t - UFxGgrILQBA ","
filelocation ":"\ / home\ / prakash\ / woodoo\ / public\ / assets\ / dist\ / support\ / attach\ / hqdefault.jpg "}]" ["time"] => string(19)
"2016-11-21 11:37:25" ["created_at"] => string(19)
"2016-11-21 11:38:11" ["updated_at"] => string(19)
"2016-11-21 11:38:11"
}["relations": protected] => array(0) {}["hidden": protected] => array(0) {}["visible": protected] => array(0) {}["appends": protected] => array(0) {}["fillable": protected] => array(0) {}["guarded": protected] => array(1) {
[0] => string(1)
"*"
}["dates": protected] => array(0) {}["dateFormat": protected] => NULL["casts": protected] => array(0) {}["touches": protected] => array(0) {}["observables": protected] => array(0) {}["with": protected] => array(0) {}["morphClass": protected] => NULL["exists"] => bool(true)["wasRecentlyCreated"] => bool(false)
}
}
}
}
}


what i'm doing wrong. please help me

Answer
@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().