0

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

3 Answers 3

1

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)

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

6 Comments

I was thinking of using lists already but wasn't sure how to do it. I will try that and let you know. Thanks to you and Nehal for your help so far!!!! Much appreciated
By the way, will you mind to explain the advantage of using form::model instead of form::open and Form::select instead of manual creation?
Sure, when you use Form::model you can pass your object as the first parameter and all the fields inside the form with the same name of your object properties will be automatically populated. Its good to use in your edit forms and stuff. Form::select its just a faster way to create your select, it will auto populate your options with the objt you pass in you second parameter, and auto select the option you put in your third parameter, whatever classes or tag atributes you want to pass you can put it inside the optional array in you last param. I hope I answered your question :)
I tried your solution nad it works great!. the only things I couldn't figure out is how to add an option for selected "Please select a Brand". Any ideas?
I forgot to add that the Please select a brand should be a null value
|
0

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

3 Comments

The old() value is getting populated fine and in this case is $brand->id but instead I would like to repopulate as old value something like this : old('brand->brand'). I know this is not right but I dont know how to do it. The reason why I put in the select option the brand->id is because I get the value and in my controller I search within the table Brands for the id and then I get other properties like price, etc. Maybe you could tell me how can I do to use brand->brand in my option value and search for that value in the Brands table?
what is your column name for brand?
My table is id | brand | brand_slug | brand_price. So the idea is the user selects the brand being populated from the DB in the select box and when the form is sent in the next view I show the price for that brand.
0

$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']) !!}

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.