比较 laravel 5 blade 中的字符串

Comparing strings in laravel 5 blade

我的 blade 文件中有这个:

 <select class="form-control" name="category" id="category">
   @foreach ($category as $h)
     @if ({{$h->id}} == {{$directory->category}})
      <option value="{{$h->id}}" selected>{{$h->name}}</option>
     @else
      <option value="{{$h->id}}">{{$h->name}}</option>
     @endif
   @endforeach
</select>

和控制器:

    $directory= Directory::find($id);
    $category = StoreCategory::all();
    return view('edit')->with('category', $category)->with('directory', $directory);

打开编辑后,我得到 "syntax error, unexpected '<'"

尝试删除 if-else 条件并且它工作正常。

ِ你不应该在你的 if 块中使用 {{ ... }}。将其更改为:

@if ( $h->id == $directory->category )