InvalidSyntax
InvalidSyntax

Reputation: 9505

Laravel: Looping through Input data in Blade

I have a form which posts to my confirmation page, on this page the user is shown the details entered and requested to either 'Post' or Go Back.

I want to know how can I iterate through all of the post data items as hidden inputs without physically writing them for each post data item.

Here is my confirm method in my controller to show you how the data is passed:

public function confirm(Request $request)
{
    $input = $array = array_except(Request::all(), array('_token'));

    return view('jobs.confirm', compact($input));
}

Here is my confirmation blade page:

{{ Input::get('title') }}
{{ Input::get('description') }}

{!! Form::open(['action' => 'JobsController@store']) !!}
    @foreach (Input as $input)
    @endforeach
{!! Form::close() !!}

My foreach loop doesn't work, can anyone explain how I can achieve the loop passing both key and value?

Upvotes: 1

Views: 5328

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 153010

First of all, you can't just loop over the class Input. Use Input::all() to get all items or work with the variable you're passing to the view.

Then, to get the key and the value, use the normal PHP foreach syntax:

@foreach($input as $name => $value)

And lastly, you obviously have to put something between the @foreach and the @endforeach tag.

For example:

@foreach($input as $name => $value)
    {{ $name }}: {{ $value }} <br/>
@endforeach

Upvotes: 3

Related Questions