且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何在Laravel中更新类别下拉菜单

更新时间:2023-11-30 14:47:22

首先,从***的角度出发,不要从视图与数据层进行通信.使用控制器将数据传递到视图.

First of all from a best practive standpoint, dont communicate with the data layer from a view. Pass the data to a view using the controller.

对于您的问题:修复if语句,您正在尝试回显条件.

For you problem: Fix the if statement, you're trying to echo the condition.

 <select class="form-control form-control-alternative" name="category">
  <option selected value="{{$category->id}}">{{$category->name}}</option>
  @foreach($$categories as $cat)
      @if($cat->id !== $category->id)
          <option value="{{$cat->id}}">{{$cat->name}}</option>
      @endif
  @endforeach
</select>

调用控制器的功能

public function __invoke(Category $category)
{
    $categories = Category::all();
    return view('machine.edit', compact('category', 'categories'));
}