Form - Passing array from controller to view - PHP - Laravel

I'm really new to Laravel and I'm not sure if I know what I'm doing. I have a form in my main view. I am passing the input to a controller and I want the data to be displayed in another view. I can't seem to get the array from the controller to the second kind. I keep getting 500 hphp_invoke. Here's where I am passing array from controller to view2.

public function formSubmit()
    if (Input::post())
        $name = Input::get('name');
        $age = Input::get('age');
        $things = array($name, $age);
        return View::make('view2', array('things'=>$things));



{{ Form::open(array('action' => 'controller@formSubmit')) }}
    <p>{{ Form::label('Name') }}
    {{ $name = Form::text('name') }}</p>
    <p>{{ Form::label('Age') }}
    {{ $age = Form::text('age') }}</p>
    <p>{{ Form::submit('Submit') }}</p>
{{ Form::close() }}


My view2.php file is very simple.

    echo $name;
    echo $age;


Then in routes.php

Route::get('/', function()
    return View::make('view1');
Route::post('view2', 'controller@formSubmit');


Why doesn't it work?


source to share

4 answers

try with ()

$data = array(
    'name'  => $name,
    'age' => $age

return View::make('view2')->with($data);


on view get: - echo $ data ['name']; echo $ data ['age'];


return View::make('view2')->with(array('name' =>$name, 'age' => $age));


get in sight: -

echo $name;
echo $age;


Read more ... here



You need to use:

return View::make('view2')->with(['name' => $name, 'age' => $age]);




and $age

in your template



Since it is $things

already array

, so you can use the following approach, but make the array associative:

$name = Input::get('name');
$age = Input::get('age');
$things = array('name' => $name, 'age' => $age);
return View::make('view2', $things);


So, you can access $name

and $age

in view

. Alternatively, you can try the following:

return View::make('view2')->with('name', $name)->with('age', $age);




In your controller use

return View::make('view2')->with($things);


In your opinion, you can now access each attribute using

@foreach($things as $thing)
    <p>{{ $thing->name }}</p>
    <p>{{ $thing->age }}</p>




All Articles