Reputation: 197
I want to pass two variables to a view, so that I can get the data from the database.
I've tried to put both in the same line but it didn't work. And I am having this error:
Undefined variable: utilizadores (View: C:\Users\ACC\Desktop\projeto\acc\resources\views\gestaoutilizadores.blade.php)
Because it only sends the variable $users
and then it returns the view.
This is my code:
public function gestaoutilizadores(){
$users = DB::select('select * FROM `users` WHERE 1');
return view('gestaoutilizadores',['users'=>$users]);
$utilizadores = DB::select('select * FROM `utilizadores` WHERE 1');
return view('gestaoutilizadores',['utilizadores'=>$utilizadores]);
}
How can I pass the variables $users
and $utilizadores
to the view?
Upvotes: 2
Views: 120
Reputation:
Try this you can use compact
public function gestaoutilizadores(){
$users = DB::select('select * FROM `users` WHERE 1');
$utilizadores = DB::select('select * FROM `utilizadores` WHERE 1');
return view('gestaoutilizadores',compact('users', 'utilizadores'));
}
Upvotes: 0
Reputation: 511
https://laravel.com/docs/4.2/responses#views
You can try this
gestaoutilizadores(){
$users = DB::select('select * FROM users WHERE 1');
$utilizadores = DB::select('select * FROM utilizadores WHERE 1');
return view('gestaoutilizadores', ['utilizadores'=>$utilizadores, 'users'=>$users]);
}
gestaoutilizadores(){
$users = DB::select('select * FROM users WHERE 1');
$utilizadores = DB::select('select * FROM utilizadores WHERE 1');
return view('gestaoutilizadores', compact ('utilizadores', 'users');
}
Upvotes: 0
Reputation: 143
public function gestaoutilizadores(){
$users = DB::select('select * FROM `users` WHERE 1');
$utilizadores = DB::select('select * FROM `utilizadores` WHERE 1');
return view('gestaoutilizadores',['utilizadores'=>$utilizadores, 'users'=>$users]);
}
Upvotes: 0
Reputation: 54841
Return array with both values in it:
return view('gestaoutilizadores', ['utilizadores'=>$utilizadores, 'users'=>$users]);
Upvotes: 3
Reputation: 26450
Whenever you return
something, the function call stops there and returns what you told it to.
You need to return the data in a single array, which can be set to a variable that holds the data (makes it a little cleaner to read if you get many variables that you need to return to the view). Then return to the view with the $data
variable as the second parameter to view()
.
public function gestaoutilizadores() {
$data = [];
$users = DB::select('select * FROM `users` WHERE 1');
$utilizadores = DB::select('select * FROM `utilizadores` WHERE 1');
$data['users'] = $users;
$data['utilizadores'] = $utilizadores;
return view('gestaoutilizadores', $data);
}
Though you can also do it inline, by doing
public function gestaoutilizadores() {
$users = DB::select('select * FROM `users` WHERE 1');
$utilizadores = DB::select('select * FROM `utilizadores` WHERE 1');
return view('gestaoutilizadores', ['users' => $users, 'utilizadores' => $utilizadores]);
}
Upvotes: 1