RomeNYRR
RomeNYRR

Reputation: 887

Laravel 5 | Select to variable

I've created a very simple form that has some select values and when you select one of the items from the list and hit submit, it goes to another page that imports a file template to confirm your selection. For some reason on the next page, instead of displaying the item name that was selected, only the row ID of the select value pops up. Is there an additional option that I need to pass through to get the value displayed in the list?

Here's my create.blade.php

 <div class="form-group">
        {!! Form::label('candy_flavors', 'Candy Flavors:') !!}
        {!! Form::select('candy_flavors', array('' => 'Select Flavor') + $candy,  null, ['class' => 'form-control'])!!}
    </div>

Here's my CandyController.Php

public function create()
{
    $candy = Candy::all()->lists('name');
    return view('candy.create', compact ('candy'));
}

public function confirm(Requests\PrepareCandyRequest $request, Guard $auth)
        {
            $candytemplate = $this->compileCandyRequestTemplate($request->all(), $auth);
            return view('candy.confirm', compact('candytemplate'));
        }

public function compileCandyRequestTemplate($data, Guard $auth)
    {
        $data = $data + [
                'name' => $auth->user()->name,
                'email' => $auth->user()->email,
            ];
        return view()->file(app_path('Http/Templates/candytemplate.blade.php'), $data);
    }

Here's my candytemplate.blade.php

@extends ('master')
@section ('content')

This is your candy selection: {{ $candy }}

@endsection

Upvotes: 0

Views: 1636

Answers (3)

RomeNYRR
RomeNYRR

Reputation: 887

So what I decided to do was make the call from my controller, and pass through the label twice. This did the trick

$accountexecutive=AccountExecutive::lists('flavor', 'flavor');
return view('candy.create', compact ('flavors'));

Upvotes: 0

Jignesh Solanki
Jignesh Solanki

Reputation: 222

 $candy=['apple'=>'apple','banana'=>'banana','mango'=>'mango'];

and now use

<div class="form-group">
        {!! Form::label('candy_flavors', 'Candy Flavors:') !!}
        {!! Form::select('candy_flavors', array('' => 'Select Flavor') + $candy,  null, ['class' => 'form-control'])!!}
    </div>

you have to specify key and value otherwise select will set value from 0

Upvotes: 1

Chris
Chris

Reputation: 58242

In confirm you can just call:

$request->get('candy_flavors')

However it will return blank, because it is attempting to return you the "value", not the "display value" of the select box. And in your case you are passing in an array of empty keys. array('' => 'Please select', '' => 'name 1', etc).

The form builder class uses the keys of the array to fill in the values.

Upvotes: 1

Related Questions