Skip to content
Snippets Groups Projects
isSKUAvailable.php 950 B
<?php

declare (strict_types = 1);

namespace App\Rules;

use App\Models\Product;
use App\Models\Retailer;
use Illuminate\Contracts\Validation\Rule;

class isSKUAvailable implements Rule
{

    public ?int $retailer_id;
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct(?int $retailer_id)
    {
        $this->retailer_id = $retailer_id;
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        $product = Product::where('sku', $value)->where('retailer_id', $this->retailer_id)->get();
        return (count($product) === 0) ? true : false;

    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'This sku is already in use.';
    }
}