0

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

3 Answers 3

1

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.

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

Comments

1
 $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

2 Comments

Thanks for the reponse Artist. The problem is the list is being generated from the Database so by switching up the variable I'm manually defining which I want to avoid doing
@RomeNYRR You can do like $arr=["a","b","c"]; $a=[]; foreach ($arr as $val) { $a[$val]=$val; }
0

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'));

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.