Laravel model binding for select dropdown

I want the selection dropdown to be selected in my edit form.

In my controller

public function edit($id)
{
    $vedit = DB::table('vehicles')->where('id', $id)->first();
    $cartype= DB::table('car_category')->pluck('cartype'); 
    return view('vehicles.edit', compact('vedit','cartype'));
}

In sight

{{ Form::label('Vehicle Type', 'Vehicle Type') }}
<select name="vehicle_type" class="form-control">
  @foreach($cartype as $cartypes)   
  <option value="{{ $cartypes}}">{{ $cartypes}}</option>
  @endforeach
</select>

How can i achieve this?

+4
source share
3 answers

By calling pluck(), an array of types is already being returned for car types.

So just use it just like the Laravel Collective:

{!! Form::label('Vehicle Type', 'Vehicle Type') !!}
{!! Form::select('vehicle_type', $cartype, null, ['class' => 'form-control']) !!}

Notice I also changed your double curly braces. Double curly braces exit the output - if the facade Formreturns HTML code, you want it to be non-isolated.

Additional information on creating drop-down lists using the Laravel Collective; https://laravelcollective.com/docs/5.4/html#drop-down-lists

, :

{!! Form::label('Vehicle Type', 'Vehicle Type') !!}
{!! Form::select('vehicle_type', $cartype, old('vehicle_type', $vedit->vehicle_type), ['class' => 'form-control']) !!}
0

Laravel ? , Laravel.

:

{!! Form::label('Vehicle Type', 'Vehicle Type') !!}
{!! Form::select('vehicle_type', $cartype, $vedit->vehicle_type ?: old('vehicle_type), ['class' => 'form-control']) !!}

, $vedit->vehicle_type . . old('vehicle_type') .

0

You can add an attribute selectedif it is selected like this:

{{ Form::label('Vehicle Type', 'Vehicle Type') }}
<select name="vehicle_type" class="form-control">
    @foreach($cartype as $cartypes) 
        <option value="{{ $cartypes}}" {{ $cartypes == $vedit->vehicle_type ? 'selected' : ''}}>{{ $cartypes}}</option>
    @endforeach
</select>
0
source

Source: https://habr.com/ru/post/1688390/


All Articles