Laravel - 从数据库中删除数据记录

时间:2017-07-06 06:46:46

标签: php laravel-5.3

我想从数据库中删除记录Photo和Photo_list但是给我错误

This is my database Product

这是我在控制器中的代码

public function deletephoto($id)
{

    $product = $this->productRepository->findWithoutFail($id);
    Product::select('photo','photo_list')->delete($product->id);

    return redirect(route('stores.index'));

}

4 个答案:

答案 0 :(得分:1)

我认为你不能删除删除特定数据。 删除用于删除行。

您需要使用以下请求更新您的表:

- (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size {
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return destImage;
}

你可以在这里看到更多:
https://laravel.com/docs/5.3/eloquent#updates
https://laravel.com/docs/5.3/eloquent#deleting-models

答案 1 :(得分:0)

public function deletephoto($id)
{

    $product = $this->productRepository->findWithoutFail($id);
    Product::Where('id','=',$product->id)->delete();

    return redirect(route('stores.index'));

}

或者你可以直接这样做

Product::find($id)->delete();

答案 2 :(得分:0)

试试这个

Product::WhereProductId($product->id)->delete();

答案 3 :(得分:0)

你可以试试这个:

public function deletephoto($id)
    {

        $product = $this->productRepository->findWithoutFail($id);
        if($product){
          $product->photo= null;
          $product->photo_list= null;
          $product->save();
        }

        return redirect(route('stores.index'));

    }
相关问题