Nur Uddin
Nur Uddin

Reputation: 1840

laravelcollective: add extra attribute to each options(Form::Select)

<select class="form-control" id="user" name="car">
  <option value="0" selected="selected">Select Cars</option>
  <option dta-img='1.jpg' value="134">Volvo</option>
  <option dta-img='2.jpg' value="135">Noah</option>
  <option dta-img='3.jpg' value="136">Marcidis</option>
  <option dta-img='4.jpg' value="143">London Express</option>
</select>

My array for options :

public function guardianList()
    {
      $list = ["Select Cars"];
        foreach ($carss->all() as $car) {
            $list[$car->id] =  $car->name;
        }
        return $list;
}

   Form::select('car',$list, null, null);

How can i add extra dynamic html attribute to all options? I want to show image infront of every option in jquery select2. For that i need to send image from Form::Select(). I there any easy way to do that without using marcos?

Upvotes: 1

Views: 789

Answers (2)

I don't know exactly since which version, but laravelcollective/html 5.8 version takes optionParameters as fifth parameter, not 4th

Upvotes: 0

Nur Uddin
Nur Uddin

Reputation: 1840

Solved the issue: Pass array in the 4th param.

$opt_attributes = collect($guardians->all())
        ->mapWithKeys(function ($item) {
            return [$item->id => ['data-img' => $item->photos->last()->name]];
        })->all();

make a array linke this and pass it as 4th parameter.

Upvotes: 2

Related Questions