laravel 7 编辑旧值未选择的形式
laravel 7 edit old value not selected form
你好,我在编辑表单中将此代码用于 select 类别,但它不起作用!
这条线不起作用
<option value="{{$category->id}}" {{ old('parent') ? 'selected' : '' }} >
并使用这个但是两个代码不起作用!
{{ in_array($category->id ,$category->child()->pluck('id')->toArray()) ? 'selected': ''}}
我的密码是
<option value="0"{{$category->parent == '0' ? 'selected': ''}}>دسته اصلی</option>
@foreach(\App\Category::all() as $category)
<option value="{{$category->id}}" {{ in_array($category->id ,$category->child()->pluck('id')->toArray()) ? 'selected': ''}}>
@if($category->parent== 0)
{{$category->name}}
@else
-- {{$category->name}}
@endif
</option>
@endforeach
我的模特属于这个
public function child()
{
return $this->hasMany(Category::class, 'parent', 'id');
}
public function get_parent()
{
return $this->hasOne(Category::class,'id','parent');
}
你需要这样做
<option value="{{ $category->id }}" {{ old('parent') == $category->id ? 'selected' : '' }} >
但是假设如果你有所有的分类并且一个分类也有编辑记录,意味着你来编辑页面只有一个分类并且你希望默认选择它。
<option value="{{ $category->id }}" @isset($cat) {{ $cat->id == $category->id ? 'selected' : '' }} @endisset >
在这种情况下,$cat
是用于编辑的单个类别,$category
是您循环浏览下拉列表中的所有类别。
UPDATED CODE
<select name="parent" id="parent">
<option value="0"{{$category->parent == '0' ? 'selected': ''}}>دسته اصلی</option>
@foreach(\App\Category::all() as $category_loop)
<option value="{{$category->id}}"
@isset($category) {{ $category->parent == $category_loop->id ? 'selected' : '' }} @endisset>
@if($category_loop->parent== 0)
{{$category_loop->name}}
@else
-- {{$category_loop->name}}
@endif
</option>
@endforeach
</select>
你好,我在编辑表单中将此代码用于 select 类别,但它不起作用! 这条线不起作用
<option value="{{$category->id}}" {{ old('parent') ? 'selected' : '' }} >
并使用这个但是两个代码不起作用!
{{ in_array($category->id ,$category->child()->pluck('id')->toArray()) ? 'selected': ''}}
我的密码是
<option value="0"{{$category->parent == '0' ? 'selected': ''}}>دسته اصلی</option>
@foreach(\App\Category::all() as $category)
<option value="{{$category->id}}" {{ in_array($category->id ,$category->child()->pluck('id')->toArray()) ? 'selected': ''}}>
@if($category->parent== 0)
{{$category->name}}
@else
-- {{$category->name}}
@endif
</option>
@endforeach
我的模特属于这个
public function child()
{
return $this->hasMany(Category::class, 'parent', 'id');
}
public function get_parent()
{
return $this->hasOne(Category::class,'id','parent');
}
你需要这样做
<option value="{{ $category->id }}" {{ old('parent') == $category->id ? 'selected' : '' }} >
但是假设如果你有所有的分类并且一个分类也有编辑记录,意味着你来编辑页面只有一个分类并且你希望默认选择它。
<option value="{{ $category->id }}" @isset($cat) {{ $cat->id == $category->id ? 'selected' : '' }} @endisset >
在这种情况下,$cat
是用于编辑的单个类别,$category
是您循环浏览下拉列表中的所有类别。
UPDATED CODE
<select name="parent" id="parent">
<option value="0"{{$category->parent == '0' ? 'selected': ''}}>دسته اصلی</option>
@foreach(\App\Category::all() as $category_loop)
<option value="{{$category->id}}"
@isset($category) {{ $category->parent == $category_loop->id ? 'selected' : '' }} @endisset>
@if($category_loop->parent== 0)
{{$category_loop->name}}
@else
-- {{$category_loop->name}}
@endif
</option>
@endforeach
</select>