Passing variable from controller to view - Laravel

52,502

Solution 1

First you should change your postView function into:

public function postView1()
{
    return Redirect::route('view2', ['name' => Input::get('name')]);
}

And your route:

Route::post('form/{name}', array('as' => 'form', 'uses'=>'HomeController@postView1'));

into:

Route::post('form', array('as' => 'form', 'uses'=>'HomeController@postView1'));

Now, you should change your view2 function into:

public function view2($name)
{
    return View::make('view2')->with('name',$name);
}

Now in your view2.blade.php you should be able to use:

<p> Hello, {{ $name }} </p>

Solution 2

You need to name the variable:

public function view2($name)
{
    return View::make('view2')->with('name', $name);
}

Solution 3

class HomeController extends Controller {
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    public function index()
    {
        $data = array (
            'title'=>'My App yo',
            'Description'=>'This is New Application',
            'author'=>'foo'
        );
        return view('home')->with($data);;
    }
}
Share:
52,502
porcupine92
Author by

porcupine92

Updated on July 16, 2022

Comments

  • porcupine92
    porcupine92 almost 2 years

    I'm trying to pass a variable from one view to a controller to another view. I'm not getting any errors, but when it gets to the last view, it doesn't show the variable like it's supposed to. In the first view, I'm just getting a name.

    {{ Form::open(array('route' => 'form', 'method'=>'post')) }}
        {{ $name = Form::text('name') }}
        {{ Form::submit('Go!') }}
    {{ Form::close() }}
    

    Here is my HomeController.php.

    public function view1()
    {
        return View::make('stuff');
    }
    
    public function postView1($name)
    {
        return Redirect::route('view2')->with($name);
    }
    
    public function view2($name)
    {
        return View::make('view2')->with($name);
    }
    

    routes.php

    Route::get('/', array('as' => 'stuff', 'uses' => 'HomeController@stuff'));
    Route::post('form/{name}', array('as' => 'form', 'uses'=>'HomeController@postView1'));
    Route::get('view2/{name}', array('as' => 'view2', 'uses' => 'HomeController@view2'));
    

    view2.blade.php

    {{ $name = Input::get('name') }}
    <p> Hello, {{ $name }} </p>
    

    So why isn't it showing up?

  • porcupine92
    porcupine92 over 9 years
    I tried that, and it still didn't work. Do you have any other ideas about what could be wrong with what I'm doing? I've been trying to figure this out for a while now, and I just can't get it to work.
  • porcupine92
    porcupine92 over 9 years
    I did that, and it gave me the error - "Undefined index: name". How would I fix that?
  • Rakesh Sharma
    Rakesh Sharma over 9 years
    check print_r($data); you have name exist in your post data? also check updated form
  • porcupine92
    porcupine92 over 9 years
    That makes sense now. But I'm still having a problem. Now, when it gets to view2, it just says "Hello, {name}" instead of actually showing the name.
  • Marcin Nabiałek
    Marcin Nabiałek over 9 years
    @porcupine92 And what exactly URL you put into browser for that? You should use for example http://localhost/yourproject/view2/porcupine92
  • porcupine92
    porcupine92 over 9 years
    When I type a name into the text field for the form and click the submit button, it goes to view2 and just says "Hello, {name}". The URL just says http://localhost/myproject/view2/%7Bname%7D. If I type http://localhost/yourproject/view2/porcupine92 for the URL, it just says "Hello, " with no name at all.
  • Marcin Nabiałek
    Marcin Nabiałek over 9 years
    @porcupine92 I've edited my answer, now data is injected as route parameter
  • porcupine92
    porcupine92 over 9 years
    I'm getting a "NotFoundHttpException" now. Could that be because of a problem with my routes?
  • Marcin Nabiałek
    Marcin Nabiałek over 9 years
    @porcupine92 And what url you use now when you get this exception? You should be precise and make sure code is exactly the same as I showed.
  • porcupine92
    porcupine92 over 9 years
    My code looks exactly like yours. When I use the url http://localhost/myproject/view2/porcupine92, it does say "Hello, porcupine92". But why won't it do that when I use the submit button? Thank you so much for your help so far!
  • porcupine92
    porcupine92 over 9 years
    Thank you so much! That worked. Hopefully someday I'll be as good as you with PHP.
  • Sumesh Ps
    Sumesh Ps over 7 years
    view page <?php echo $title; ?>
  • NorthStarCode
    NorthStarCode over 5 years
    Good answer. This is one of the cleanest solutions.