mardi 21 juillet 2020

How to validate relation in request class in laravel?

In my system, a customer can create a product and purchase an order. That product can be added to the purchase order as a purchase order product. The scenario is: a customer can only add his own product to the purchase order as a purchase order product.

I want to validate that this in the Request class.

purchase order product Request:

public function rules()
    {
        return [
            'purchase_order_id' => 'required|numeric|exists:purchase_orders,id',
            'product_id' => 'required|exists:products,id',

        ];
    }

Here I need to check that the user can add this product, i.e. that this product belongs to that customer.

Product Model:

public function customers()
    {
        return $this->belongsTo('App\Models\Customer', 'customer_id');
    }

purchase order:

 public function customers()
    {
        return $this->belongsTo('App\Models\Customer', 'customer_id');
    }

    public function purchase_order_products()
    {
        return $this->hasMany('App\Models\PurchaseOrderProduct', 'purchase_order_id');
    }

purchase order product model:

public  function products()
    {
        return $this->belongsTo('App\Models\Product', 'product_id');
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire