Passing variable from controller to view - Laravel

我们两清 提交于 2019-11-30 09:27:18

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>

You need to name the variable:

public function view2($name)
{
    return View::make('view2')->with('name', $name);
}
Sumesh Ps
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);;
    }
}

Here's what other answers are missing, straight from Laravel docs:

Since the with method flashes data to the session, you may retrieve the data using the typical Session::get method.

So instead of {{$name}} write {{Session::get('name')}}.

Try form will be if you using POST method why setting variable in route it will get directly on your function with post data.

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

route :-

Route::post('form','HomeController@postView1');

controller function :-

public function postView1() {
  $data = Input::all();
  return Redirect::route('view2')->with('name', $data['name']);
}

and get data on view2 :-

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

For more follow HERE

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!