FaizFurios52
FaizFurios52

Reputation: 77

Laravel - Delete data record from Database

I want to delete record Photo and Photo_list from Database but give me error

This is my database Product

This is my Code in Controller

public function deletephoto($id)
{

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

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

}

Upvotes: 0

Views: 2023

Answers (4)

Minar_Mnr
Minar_Mnr

Reputation: 1405

You can try this :

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'));

    }

Upvotes: 0

Benjamin Brasseur
Benjamin Brasseur

Reputation: 558

I don't think you can delete specific data with delete. Delete is used to remove a row.

You will need to update your table with a request like that :

public function deletephoto($id)
{

    $product = $this->productRepository->findWithoutFail($id);
    Product::where('id', 100)->update(['photo' => NULL, 'photo_list' => NULL]);

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

}

You can see more here :
https://laravel.com/docs/5.3/eloquent#updates
https://laravel.com/docs/5.3/eloquent#deleting-models

Upvotes: 1

Try this

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

Upvotes: 0

FULL STACK DEV
FULL STACK DEV

Reputation: 15941

public function deletephoto($id)
{

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

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

}

Or you can directly do this

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

Upvotes: 0

Related Questions