lundi 29 août 2016

What is the best practice to delete all relationships in Laravel?

I need to delete all relationships of product. I got error when I trying to delete item with relationships. Here is my code:

public function getDelete(Product $product)
{
    $name = $product->name;

    $product->Field()->Data()->delete();
    $product->Field()->delete();
    $product->delete();

    return redirect('products')->with('success', 'Product <b>'.$name.'</b> successfully deleted.');
}

Here is models:

public function Field()
{
    return $this->hasMany("\App\ProductField", 'product_id','id');
}

public function Data()
{
    return $this->hasMany("\App\ProductFieldData", 'product_field_id','id');
}

BadMethodCallException in Builder.php line 2101: Call to undefined method Illuminate\Database\Query\Builder::Data()

This error I got If product don't have a ProductFieldData. But I think its must be skipped if don't have relationships data?

How to delete product all relationships?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire