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);;
}
}
Author by
porcupine92
Updated on July 16, 2022Comments
-
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 over 9 yearsI 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 over 9 yearsI did that, and it gave me the error - "Undefined index: name". How would I fix that?
-
Rakesh Sharma over 9 yearscheck print_r($data); you have name exist in your post data? also check updated form
-
porcupine92 over 9 yearsThat 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 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 over 9 yearsWhen 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 typehttp://localhost/yourproject/view2/porcupine92
for the URL, it just says "Hello, " with no name at all. -
Marcin Nabiałek over 9 years@porcupine92 I've edited my answer, now data is injected as route parameter
-
porcupine92 over 9 yearsI'm getting a "NotFoundHttpException" now. Could that be because of a problem with my routes?
-
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 over 9 yearsMy 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 over 9 yearsThank you so much! That worked. Hopefully someday I'll be as good as you with PHP.
-
Sumesh Ps over 7 yearsview page <?php echo $title; ?>
-
NorthStarCode over 5 yearsGood answer. This is one of the cleanest solutions.