简体   繁体   中英

what is the correct method to fill select use jQuery and laravel?

I have a form for adding Salaries, in this form there is select Fonction_id foreign key from table function in table Salaries (belongsTo relation between them), I want to fill this select, knowing that I use laravel and jquery, what is the good method to do.

<div class="form-group col-md-3">
     <select class="form-control" name="fonction_id">
          <option></option>
          <option> fonction_id </option>
      </select>
</div>

It do not need to jquery.Do it as bellow:

First select your objects from database in controller and pass it from controller to view by compact like this:

$models = Yourmodel::All();
return view('yourview',compact('models');

And in your view render it in foreach as this:

  <select>
      @foreach($models as $model)
        <option>{{$model->yourfield}}</option>@endforeach </select>         

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM