Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variable from controller to view - Laravel

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?

like image 772
porcupine92 Avatar asked Oct 14 '14 05:10

porcupine92


People also ask

How do you pass a variable to another page in Laravel?

There are two ways to pass variables between web pages. The first method is to use sessionStorage, or localStorage. The second method is to use a query string with the URL.

How do I return a view from a controller in Laravel?

Views may also be returned using the View facade: use Illuminate\Support\Facades\View; return View::make('greeting', ['name' => 'James']); As you can see, the first argument passed to the view helper corresponds to the name of the view file in the resources/views directory.

How can we get data from database in controller in Laravel?

After configuring the database, we can retrieve the records using the DB facade with select method. The syntax of select method is as shown in the following table. Run a select statement against the database.


2 Answers

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>
like image 200
Marcin Nabiałek Avatar answered Sep 21 '22 06:09

Marcin Nabiałek


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);;
    }
}
like image 22
Sumesh Ps Avatar answered Sep 18 '22 06:09

Sumesh Ps