有人可以告诉我当我尝试在 Laravel 项目中编辑帖子时如何获得选定的值。 所以这里有部分代码
后.php:
public function category(){
return $this->belongsTo('App\Category');
}
类别.php:
public function posts(){
return $this->hasMany('App\Post');
}
后控制器.php:
$post = Post::findOrFail($id);
$categories = Category::all();
$tags = Tag::all();
return view('admin.posts.edit', ['post' => $post, 'categories' => $categories, 'tags' => $tags]);
帖子/edit.blade.php:
<div class="form-group">
<label for="category_id">Selecet category</label>
<select name="category_id" id="category_id" class="form-control">
<option disabled>List of available post categories</option>
@foreach($categories as $category)
<option value="{{ $category->id }}" {{$category->id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
@endforeach
</select>
</div>
慕桂英4014372
子衿沉夜