php - Laravel 5.5 编辑后无法删除类别

标签 php laravel-5 laravel-5.5

当我编辑帖子时,我想删除已取消的类别。我使用同步方法,但它不起作用..

编辑 Blade

<select class="form-control m-select2" id="m_select2_3" name="categories[]" multiple="multiple">
  <optgroup label="Kategoriler">
     @foreach($categories as $category)
        <option value="{{$category->id}}"  {{ in_array($category->id,$categoriesPost) ? 'selected' : '' }}>{{$category->category_name}}
  </option>
     @endforeach
   </optgroup>
 </select>

和 Controller

public function update(StoreBlogPost $request, $id){

        $post = Post::find($id);

        $post->title = $request->title;
        $post->caption = $request->caption;
        $post->content = $request->input('content');

        $post->save();
        $post->categories()->sync($request->categories, false);

        return redirect()->route('posts.index');
    }

有什么建议吗?

最佳答案

您应该将sync的第二个参数设置为true

从代码中,

/**
 * Sync the intermediate tables with a list of IDs or collection of models.
 *
 * @param  \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array  $ids
 * @param  bool   $detaching
 * @return array
 */
public function sync($ids, $detaching = true)
{
    ....
}

这意味着,$ids 中不存在的 ID 将被分离。

关于php - Laravel 5.5 编辑后无法删除类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47453811/

相关文章:

php - "->"在php中是什么意思?

laravel - 调用未定义的方法Illuminate\Auth\GenericUser::save()

php - laravel 无法识别 ISO8601 格式的日期时间

laravel - 在 laravel api 中将响应消息放在哪里?

php - 使用 javascript/jquery 在页面加载时提交表单

php - 更新用户详细信息,sql server 2008 sqlsrv

laravel - Laravel 中的数据表分页

php - 拉维尔 5.2 : csrf token doesn't work

php - Laravel 5.5 : htmlspecialchars() expects parameter 1 to be string, 数组给出

php - 使用主键以外字段的 Yii 模型关系