Passing a variable from a controller to view - Laravel

I am trying to pass a variable from one view to a controller in another view. I don't get any errors, but when it gets to the last view, it does not show the variable as intended. At first glance, I just get the 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 doesn't he appear?

+5
source share
5 answers

First you must change your postView function to:

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

And your route:

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

in

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

Now you should change your view2 function to:

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

Now in your view2.blade.php you can use:

 <p> Hello, {{ $name }} </p> 
+10
source

You need to specify a variable:

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

The request form will be, if you use the POST method to set a variable in the route, it will work directly with 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 the data on view2: -

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

See HERE for more details.

0
source

There are no other answers here, directly from Laravel docs :

Since the method with blinking data for the session, you can get the data using the usual Session :: get method.

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

0
source
 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);; } } 
0
source

Source: https://habr.com/ru/post/1204616/


All Articles