File "ValidateProductPrice.php"

Full Path: /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Rules/ValidateProductPrice.php
File size: 1.11 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace App\Rules;

use App\Models\ProductPrice;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\InvokableRule;

class ValidateProductPrice implements InvokableRule, DataAwareRule
{

    /**
     * All of the data under validation.
     *
     * @var array
     */
    protected $data = [];

    /**
     * Set the data under validation.
     *
     * @param  array  $data
     * @return $this
     */
    public function setData($data)
    {
        $this->data = $data;

        return $this;
    }

    /**
     * Run the validation rule.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     * @return void
     */
    public function __invoke($attribute, $value, $fail)
    {
        $product_id = str_replace('price_id', 'product_id', $attribute);
        $price = ProductPrice::where(['id' => $value, 'product_id' => request($product_id)])->first();
        if (!$price) {
            return $fail('Invalid price id selected for product #:position.');
        }
    }
}