I have to tables 'users' and 'departments' and I want to get data from both tables and put in html form. here is my code which not working because there is something missing in it. here is the code of controller:
public function showProfileForm($id){
$user = DB::table('users')->where('id','=',$id)->get();
$dpt = DB::table('departments')->orderBy('department_name','asc')->get();
return view('profile.showProfile')->with(['selDpt', $dpt, 'user', $user]);
}
The get() method retrieve data as a multi-dimensional array. So when you will retrieve only one array then you have to use first() method at the end of the query. So just Change the query from
$user = DB::table('users')->where('id','=',$id)->get();
to
$user = DB::table('users')->where('id','=',$id)->first();
Now, You have to pass data by array
. So just change the line
return view('profile.showProfile')->with(['selDpt', $dpt, 'user', $user]);
To
return view('profile.showProfile')->with(['selDpt' => $dpt, 'user' => $user]);
shoProfile.blade.php
You have to use $selDpt
& $user
variable for use or echo data. Hope it will work.
Learn more at https://laravel.com/docs/5.4/views#passing-data-to-views
You can use this syntax to pass multiple variables to a view:
return view('profile.showProfile', [
'selDpt' => $dpt,
'user' => $user
]);
Or you can use PHP function compact()
. http://php.net/compact
$user = DB::table('users')->where('id','=',$id)->get();
$dpt = DB::table('departments')->orderBy('department_name','asc')->get();
return view('profile.showProfile', compact('dpt', 'user'));
You will have $user
and $dpt
in your view.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.