Alex
Alex

Reputation: 79

How to get different column values for one row

I have a multi-step form in laravel where I pass the session variables from one view to the other and everything works ok.

This is part of my form

<div class="panel-body">
            {!! Form::open(array('url' => '/post-quote')) !!}

                <div class="form-group">
                  {!! Form::Label('brand', 'Brand:') !!}
                  <select class="form-control" name="brand" id="brand">
                    <option value="{{Input::old('brand')}}" selected="selected">{{Input::old('brand', 'Please select a Brand')}}</option>
                    @foreach($brands as $brand)
                      <option value="{{$brand->id}}">{{$brand->brand}}</option>
                    @endforeach
                  </select>
                </div>  

Here is part of my controller

public function quote()
{               
    $brands = Brand::orderBy('brand')->get();       
    return View::make('frontend.step1', compact('brands'));
}

The issue I got is that I am passing the id for each option value in the select and after validation, the Input::old populates the id value and it doesn't look right to display a number instead of the brand name.

Question: is there anyway to get input::old with the {{$brand->brand}} instead of the id?

My other option is to pass as a value for each option the brand name, however I dont know how to get the id for my input in the controller.

I hope you get what I am trying to achieve.

Thanks

Upvotes: 0

Views: 76

Answers (3)

Alex
Alex

Reputation: 79

$brands = Brand::orderBy('brand')->lists('brand','id');
return View::make('frontend.step1', compact('brands')); Then in your view you can use the Form::select instead of creating the select manually. Something like this:

{!! Form::select('brand',$brands, Input::old('brand'),['class' => 'form-control']) !!}

Upvotes: 0

cbcaio
cbcaio

Reputation: 464

Did you try passing the brands as an array? Like,

$brands = Brand::orderBy('brand')->lists('brand','id');       
    return View::make('frontend.step1', compact('brands'));

Then in your view you can use the Form::select instead of creating the select manually. Something like this:

{!! Form::select('brand',$brands, Input::old('brand'),['class' => 'form-control']) !!}

Give it a try and let me know if it works, there are some others things you can try, like using Form::model instead of Form::open (I would go with this approach)

Upvotes: 1

Nehal Hasnayeen
Nehal Hasnayeen

Reputation: 1223

you can pass data to next user request by using flash method like this

$request->flash()

then retrieve it in blade template with global old helper function which gives you old input data,

{{ old('brand') }}

Upvotes: 0

Related Questions