php - Get SQL Column Value value from Nested Array -
to explain better here code:
$mydate=carbon::now()->addhours(8); $newdate=$mydate->todatestring(); $myquery = db::table('attendances')->where('date_only', '=', $newdate)->orderby('logon','asc')->get(); $counter=0; foreach ($myquery $newquery) { $query1[$counter]=$newquery->user_id; $finalquery[$counter]= db::table('employees')->where('id', '=', $query1[$counter])->get(); $counter++; } var_dump($finalquery[2]);
the output of code this:
array(1) { [0]=> object(stdclass)#160 (6) { ["id"]=> int(23) ["firstname"]=> string(3) "kim" ["lastname"]=> string(7) "samsung" ["position"]=> string(10) "programmer" ["created_at"]=> string(19) "2014-11-25 07:21:31" ["updated_at"]=> string(19) "2014-11-25 07:21:31" } }
what want access 1 of value tried
//i change var_dump($finalquery[2]); //to var_dump($finalquery[2]->firstname);
but error. error code 500
try this
// not necessary foreach ($myquery $key => $newquery) { $query1[$key]=$newquery->user_id; $finalquery[$key]= db::table('employees')->where('id', '=', $query1[$key])->get(); } // necessary print_r($finalquery->toarray()); // if array of objects i'm converting array // or print_r($finalquery);
edit: buddy suggestion use joins
single query data need not use foreach
several queries reference
$myquery = db::table('attendances')->select('employees.id emp_id','attendances.id att_id',/*your fields*/) ->leftjoin('employees', 'employees.id','=','attendances.id') ->where('date_only', '=', $newdate) ->orderby('attendances.logon','asc')->get(); print_r($myquery->toarray());
in controller
return view::make('home', array('mydata' => $finalquery));
in view: (for blade)
@if($mydata && count($mydata) > 0) @foreach($mydata $data) {{ $data->id }}<br> {{ $data->name }}<br> {{ $data->etc }}<br> @endforeach @else <p>sorry no data display</p> @endif
Comments
Post a Comment