0

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?

6
  • You mean by placing it here? public function getDropDownList($user, $char_name) { var_dump($char_name); return View::make('layout.profile') ->with('char_name', $char_name); } It does nothing. Same error, no additional messages. Commented Feb 25, 2014 at 23:03
  • I have deleted the drop down list from the view and did as you told me. The variable seems empty. It doesn't write anything on the browser. But why is it empty? Commented Feb 25, 2014 at 23:13
  • Well that variable comes from your second URL parameter. That's what you wrote in your routes: Route::get("/{user}/{char_name}", array(. What do you pass in the URL? Did you posted your full getDropDownList function? What are you trying to accomplish? Commented Feb 25, 2014 at 23:26
  • Yes. I have a post route which queries the database and a get route which tries to bring the new values to the view. First I will want to populate a drop down list with values from the database. Second I want to get the selected value and just write it in the view. Something similar to. You selected {{$char_name}}. The idea behind this is that I will try to append the $char_name to a new URL. I need to do this, this way, because I need that variable. I know how to populate the drop down list, but that solution is not viable for what I am trying to do. Commented Feb 25, 2014 at 23:33
  • I always tried to bypass what I am doing right now, by quering the database directly from the view and what-not, but no more. It really frustrating because I can't organize my code because of this issue. I can give you an example that works and I don't know why. Commented Feb 25, 2014 at 23:39

2 Answers 2

0

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:

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Where are you setting the session variables?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.