routes.php
Route::post("/{user}/{char_name}", array(
'as' => 'char-profile-post',
'uses' => 'ProfileController@postDropDownList'));
Route::get("/{user}/{char_name}", array(
'as' => 'char-profile-get',
'uses' => 'ProfileController@getDropDownList'));
ProfileController.php
public function postDropDownList($user, $char_name) {
$user = Auth::user();
$char_name = Input::get('choose');
$char_name_obj = User::find($user->id)->characters()->where('char_name', '=', $char_name)->first();
return Redirect::route('char-profile-get', array(Session::get('theuser'), $char_name_obj->char_name));
}
public function getDropDownList($user, $char_name) {
return View::make('layout.profile')
->with('char_name', $char_name);
}
Snippet from layout.profile View
<form action="{{ URL::route('char-profile-post') }}" method="post">
<select name="choose">
<option value="choose">Choose a character</option>
@foreach($char_name as $c)
<option> {{ $c->char_name }} </option>
@endforeach
</select>
<input type="submit" value="Ok">
{{ Form::token() }}
</form>
ErrorException
Undefined variable: char_name
(View: C:\wamp\www\CaughtMiddle\tutorial\app\views\layout\profile.blade.php)
When writing the following I still receive this error.
<option> {{ $char_name['char_name'] }} </option>
My question is pretty obvious. Am I doing something wrong, or Laravel is truly incapable to send a couple of variables from the controller to the view?
Laravel can absolutely send multiple variables to a view, and here's some code showing how I do it in a project of mine:
Route:
Route::get('play/{id}', array('before' => 'loggedin', 'uses' => 'GameController@confirmEntry'));
As you can see, in this route, I expect the variable id
to be passed to my controller.
Controller:
public function confirmEntry($id){
//stuff that doesn't matter
$data = array('puzzle' => $puzzle,
'user' => $user,
'wallet' => $wallet);
return View::make('game.solver-confirm', $data);
}
In the above example, since I list $id
as a parameter, and I have id
in my route, $id
will be set to whatever it was in the route. For instance, if I went to play/56
, $id
would be equal to 56
.
View:
@foreach ($puzzle->tile as $tile)
<li>a list gets made here</li>
@endforeach
You can see in the controller, I passed $puzzle
(along with a few other variables) to the view, and then I call them just like you showed in my view.
Issues with yours:
The first issue I see is that you accept two variables from your route, and then overwrite them. That doesn't make much sense to me:
public function postDropDownList($user, $char_name) {
$user = Auth::user();
$char_name = Input::get('choose');
//more stuff here
}
Why bother passing $user
and $char_name
only to overwrite them?
That makes me thing that the issue you're having is because your data is structured poorly. I'd suggest doing a var_dump
or print_r
to see how $char_name
is actually being structured.
References:
Ok, so here is how I partially resolved my issue.
routes.php
Route::group(array('prefix' => 'user'), function()
{
Route::post('/{user}/{char_name}/selectedok', array(
'as' => 'char-profile-post',
'uses' => 'ProfileController@postDropDownList'));
Route::get('/{user}/{char_name}/selectedok', array(
'as' => 'char-profile-get',
'uses' => 'ProfileController@getDropDownList'));
});
ProfileController.php
public function getDropDownList() {
return View::make('layout.profile');
}
public function postDropDownList($user, $char_name) {
if (Auth::check())
{
$user = Auth::user();
$selected_char = Input::get('choose');
$char_name = User::find($user->id)->characters()->where('char_name', '=', $selected_char)->first();
return Redirect::route('char-profile-get', array($user->username, $char_name->char_dynasty, $char_name->char_name))
->with('user', $user->username)
->with('charname', $char_name->char_name)
->with('dynastyname', $char_name->char_dynasty);
}
}
Yes I know I redirect to 'char-profile-get' with 3 parametres, but I only need 2. It doesn't bother me. As you observe, I redirect with ->with. The only way I will print the data in the view is by doing this in the view:
Your chosen character is {{ Session::get('charname') }} {{ Session::get('dynastyname')}}
If I don't overwrite the variables in postDropDownList($user, $char_name) my URL will literally look like this:
http://localhost/CaughtMiddle/tutorial/public/index.php/user/%7Buser%7D/%7Bchar_name%7D/selectedok
instead of this:
http://localhost/CaughtMiddle/tutorial/public/index.php/user/SerbanSpire/Spirescu/selectedok?Serban
So the verdict. The parametres in the functions will never receive any data. I don't even know how to describe them, the variables are inexistent, even if I redirect to the get route with the correct data. var_dump($user) or var_dump($char_name) doesn't print anything on the browser, dd($user) dd($char_name) also doesn't print anything nor does print_r().
Is my solution a viable one? By redirecting using ->with and storing the data in Session? Because this solution will drive me into using Session::get() alot! Stop me if I am wrong.