我如何在Laravel中更新类别下拉菜单 [英] How can i update category dropdown menu in laravel
本文介绍了我如何在Laravel中更新类别下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的类别下拉列表代码,
<div class="control-group">
<label class="control-label">Under Category </label>
<div class="controls">
<select name="category_id" id="category_id" style="width:220px;">
<option value='' selected disabled>Select</option>
@foreach(App\Category::all() as $cat)
<option value="{{$cat->id}}">
@if {{ $cat->parent ? '--' . $cat->name : $cat->name }}
selected @endif>{{ $cat->parent ? '--' . $cat->name : $cat->name }}
</option>
@endforeach
</select>
</div>
</div>
对于更新类别,我只使用
for update category i just use
@if {{ $cat->parent ? '--' . $cat->name : $cat->name }}
selected @endif>{{ $cat->parent ? '--' . $cat->name : $cat->name }}
但是给出了这样的错误:-
but gives an error like that :-
ErrorException(E_ERROR)语法错误,意外的':',期望'('(看法:F:\ laragon \ www \ flipcart \ resources \ views \ admin \ products \ editproduct.blade.php)
ErrorException (E_ERROR) syntax error, unexpected ':', expecting '(' (View: F:\laragon\www\flipcart\resources\views\admin\products\editproduct.blade.php)
推荐答案
首先,从最好的角度出发,不要从视图与数据层进行通信.使用控制器将数据传递到视图.
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'));
}
这篇关于我如何在Laravel中更新类别下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文