Current File : /home/tradevaly/demo.tradevaly.com.bd/app/Rules-bk/UniqueCheck.php
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use Cookie;

class UniqueCheck implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    private  $tableModelName;
    private  $fieldLabelTextEn;
    private  $fieldLabelTextBn;
    private  $columnName;
    private  $editId;
    private  $columnNameCustom;
    public function __construct($tableModelName, $columnName, $fieldLabelTextEn, $fieldLabelTextBn, $editId, $columnNameCustom)
    {
        //dd($editId);
        $this->tableModelName = $tableModelName;
        $this->columnName = $columnName;
        $this->fieldLabelTextEn = $fieldLabelTextEn;
        $this->fieldLabelTextBn = $fieldLabelTextBn;
        $this->editId = $editId;
        $this->columnNameCustom = $columnNameCustom;
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {

        //$langId = Cookie::get('langId');
        $langId = 1;
        if($langId == 2){
            $value = cv_bn_to_en($value);
        }
        $conditions = [];
        $conditions = $this->columnName;
        
        if( !empty($this->editId) ){
            if( !empty($this->columnNameCustom)){
                $conditions = [[$this->columnName, $value],[$this->columnNameCustom,'!=', $this->editId]];
            }else{
                $conditions = [[$this->columnName, $value],['id','!=', $this->editId]];
            }
        }
        //dd($conditions);
       $existsData = $this->tableModelName::where($conditions)
            ->count();
            if($existsData > 0){
                return false;
        }
        
      
        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        //$langId = Cookie::get('langId');
        $langId = 1;
        $msg = '';
        if($langId ==1){
           $msg =  $this->fieldLabelTextEn.' has already been taken.';
        }else{
            $msg =  $this->fieldLabelTextBn.' পূর্বে সংরক্ষন করা হয়েছে ।';
        }

        return $msg;
        
    }
}