mercredi 10 juillet 2019

How to delete products but not orders associated with that product in Laravel?

I would like a seller to be able to delete/edit a product he listed. But if that product has been ordered by customer, i would like the order to stay the same and not change. How can i accomplish this?

Right now when i update/delete it affects the orders submitted earlier. This is how i delete products right now.

public function destroy($id) 
{ 
    $userId = Auth::user()->id; 
    $deleteData=product::where('seller_id', $userId)->findOrFail($id); 
    $deleteData->delete(); 

    return redirect()->back(); 
}

Here is the relationship

public function user()
{
    return $this->belongsTo('App\User', 'seller_id');
}

public function products()
{
    return $this->belongsToMany('App\Product')->withPivot('quantity','total','Subtotal');
}

public function orderItems()
{
    return $this->hasMany('App\OrderProduct');
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire