简体   繁体   中英

How to pass value from model to select input use laravel eloquent

I have model with attributes category i want pass 1 data to select tag

Category option

Category
1. Fruit
2. Vegetables
3. Meat

example data

name category
Apple 1

how i can pass apple category to select tag

<select class="form-control form-select" name="category" aria-label="Default select example">
  <option value=""></option>
  <option value="1">fruit</option>
  <option value="2">vegetables</option>
  <option value="3">meat</option>
</select>

You didn't provide much details but I guess you meant something like this:

In your controller action

$categories = Category::pluck(‘name’, ‘id’)->toArray();

And in your view


<select class="form-control form-select" name="category" aria-label="Default select example">
  <option value=""></option>
  @foreach($categories as $key => $category)
    <option value="{{ $key }}">{{ $name }}</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