vipin93's avatar

How to set Dropdown list value in form model binding in L4.2

I try to make form where user can update profile but problem is that when they updated profile after it selected dropdownlist value not showing any one know how to fix it?? thanks

0 likes
8 replies
deadlockgB's avatar

did you specify a name="" attribute on the dropdown list? So it looks kind of like this:

<select id="dropdown" name="dropdown">
    <option value="0">Displayname</option>
</select>
vipin93's avatar

@deadlockgB my form like this

@extends('layouts.master')

@section('content')

   <h1 class="profile-edit">Edit your profile</h1>
   <div class="row">
     @include('layouts.partials.errors')
     {{  Form::model($user->profile, ['method' => 'PATCH', 'route' => ['profile.update', $user->username] ,'files' =>true]) }}
                <div class="col-md-4">

                   
                  @include('profile.partials.about-form')
               

                  </div>

                 <div class="col-md-3">

                  @include('profile.partials.address-form')
                   {{ Form::submit('Update Profile', ['class' => 'btn btn-primary']) }}
                 </div>
                 <div class="col-md-5">
                   @include('profile.partials.image')
                 </div>
              
                  
      {{ Form::close() }}

    </div>
@stop

and @include('profile.partials.about-form')

   <div class="form-group">
                     <lable for="storename">Shop Name:</lable>
                     {{ Form::text('storename', null, ['class' => 'form-control']) }}
                 </div>
                  
                 <div class="form-group">
                     <lable for="category">Select your shop category:</lable>
                     <select name="category_id" id="category" class="form-control" >
                      <option value="-1">Select category</option>
                      @foreach($categories as $category )
                      <option value="{{ $category->id }}">{{ $category->name }}</option>
                       @endforeach
                    </select> 
                     
                 </div>

                 <div class="form-group">
                     <lable for="subcategory_id">Select your shop subcategory:</lable>
                     <select name="subcategory_id" id="subcategory" class="form-control">
                    
                     <option value="-1">Select subcategory</option>
                      <option value=""></option>
                    
                    </select> 
                     
                 </div>
Penderis's avatar
Level 3

to use model binding you need to use blade, the key-value needs to be an array as the second parameter

//View
 <div class="field">
    {{Form::label('auction_date_id','Preferred Auction Date')}}
    {{Form::select('auction_date_id',$auctiondates)}}
</div>

//Controller
  $getAuctionDates = AuctionDate::lists('auction_date_time','id');
return View::make(edit.view)->with('auctiondates',$getAuctionDates);

all it wants is that array. Also your field names must be the same as the Schema names you are passing it.

1 like
vipin93's avatar

@Penderis but i have two dropdown list where subcategory dependent on first when i try like this

<div class="form-group">
                     <lable for="subcategory_id">Select your shop subcategory:</lable>
                     {{ Form::select('subcategory_id', null, ['class' => 'form-control' ,'id' => 'subcategory']) }}
                                                                         
            </div>

I got error

Invalid argument supplied for foreach()

first dropdown list working fine and its value store in model method but what about second

Penderis's avatar

I do not see that you would have a problem if you ensure that both follow the same pattern, query DB->if possible two separate queries if need be so you can receive your values as an array via the lists method (Category::lists('value','key')) straight from the queries then if you did it as seperate you will

//Do not be scared to use multiple with statements to make what you are doing more clear.
View::make('page')->with('categories',$categories)->with('subcategories',$subcategories)

As for the foreach error I think it is because of the null which should be the array pair of key-value. Unfortunately do not have my server running to test any code.

vipin93's avatar

@Penderis I'm using ajax method for dependent dropdown list like this

$('#category').change(function()
{
    $.get('/categories/' + this.value + '/subcategory.json', function(states)
    {
        var $subcategory = $('#subcategory');

        $subcategory.find('option').remove().end();

        $.each(states, function(index, subcategory) {
            $subcategory.append('<option value="' + subcategory.id + '">' + subcategory.name + '</option>');
        });
    });
})

and in my route

Route:: get('categories/{code}/subcategory.json', function($code)
{
    return Subcategory::where('category_id', $code)->get();
});

Please or to participate in this conversation.