3

I'm really new to Laravel, and I'm not sure that I know what I'm doing. I have a form in my main view. I'm 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 view. I keep getting 500 hphp_invoke. Here's where I'm passing the array from the 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));
    }
}

view1.blade.php

{{ 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 really simple.

<?php
    echo $name;
    echo $age;
?>

Then in routes.php

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

Why isn't this working?

4 Answers 4

7

try with()

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

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

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

or

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

get on view :-

echo $name;
echo $age;

For more Follow here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help. That doesn't seem to be working though. Could it be the route that I have? It's Route::post('view2','controller@formSubmit'); Is that wrong?
I added more of my code. Nothing that I change seems to be working. Do you see anything else wrong with what I have?
3

You need to use:

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

to use

$name and $age in your template

Comments

2

Since $things is already an array so you may use 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 your view. Also, you may try this:

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

Comments

2

In your controller, use

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

In your view, you can now access each attribute using

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.