Laravel 7 - Problem with unique constraint on update - php

I'm trying to add unique validation to my model, but there is an error when I tried to update the data.
The table:
acq_m_budgets
==================================
budget_id serial NOT NULL,
budget_code character varying(15) NOT NULL,
budget_name character varying(100) NOT NULL,
ma_code character varying(10),
start_period timestamp without time zone NOT NULL,
end_period timestamp without time zone NOT NULL,
budget numeric(16) DEFAULT 0,
credit numeric(16) DEFAULT 0,
debet numeric(16) DEFAULT 0,
balance numeric(16) DEFAULT 0,
reserve numeric(16) DEFAULT 0,
created_by character varying(100) NOT NULL,
created_on timestamp without time zone DEFAULT now(),
updated_by character varying(100) NOT NULL,
updated_on timestamp without time zone DEFAULT now(),
CONSTRAINT PK_AcqMBudgets PRIMARY KEY (budget_id),
CONSTRAINT UN_AcqMBudgets UNIQUE (budget_code)
My model: AcqMBudgets.php
class AcqMBudgets extends Model
{
public $timestamps = false;
protected $primaryKey = 'budget_id';
public $sortable = ['budget_code', 'budget_name', 'ma_code', 'balance', 'updated_on'];
protected $fillable = ['budget_code', 'budget_name', 'ma_code', 'start_period', 'end_period', 'budget', 'credit', 'debet', 'balance', 'reserve', 'created_by', 'created_on', 'updated_by', 'updated_on'];
protected $attributes = [
'budget' => 0,
'credit' => 0,
'debet' => 0,
'balance' => 0,
'reserve' => 0,
];
public static function createRules()
{
return [
'budget_code' => 'required|unique:acq_m_budgets,budget_code|max:15',
'budget_name' => 'required|max:100',
'ma_code' => 'max:10',
'start_period' => 'required',
'end_period' => 'required',
];
}
public static function updateRules($id)
{
return [
'budget_code' => 'required|unique:acq_m_budgets,budget_code,' . $id . '|max:15',
'budget_name' => 'required|max:100',
'ma_code' => 'max:10',
'start_period' => 'required',
'end_period' => 'required',
];
}
}
My Controller: BudgetController.php
...
public function create(Request $request)
{
$validateData = $request->validate(AcqMBudgets::createRules());
$model = new AcqMBudgets;
$post = $request->only($model->getFillable());
$post['start_period'] = (!empty($post['start_period'])) ? date('Y-m-d', strtotime(str_replace('/', '-', $post['start_period']))) : null;
$post['end_period'] = (!empty($post['end_period'])) ? date('Y-m-d', strtotime(str_replace('/', '-', $post['end_period']))) : null;
$model->fill($post);
$model->save();
return redirect()->route('acq.view.master.budget', ['id' => $model->budget_id, 'rf' => 'a']);
}
...
public function update($id, Request $request)
{
$validateData = $request->validate(AcqMBudgets::updateRules($request->input('budget_id')));
$model = AcqMBudgets::find($id);
$post = $request->only($model->getFillable());
$post['start_period'] = (!empty($post['start_period'])) ? date('Y-m-d', strtotime(str_replace('/', '-', $post['start_period']))) : null;
$post['end_period'] = (!empty($post['end_period'])) ? date('Y-m-d', strtotime(str_replace('/', '-', $post['end_period']))) : null;
$model->fill($post);
$model->save();
return redirect()->route('acq.view.master.budget', ['id' => $model->budget_id, 'rf' => 'e']);
}
...
On the model, I already separated the rules for create and update method. The difference is in the updateRules(), there is a primary key parameter which is needed in the array of rules.
On the controller, on update function, there is an error which stated: SQLSTATE[42703]: Undefined column: 7 ERROR: column "id" does not exist LINE 1: ...from "acq_m_budgets" where "budget_code" = $1 and "id" <> $2 ^ (SQL: select count(*) as aggregate from "acq_m_budgets" where "budget_code" = N01 and "id" <> ).
The primary key I used is integer and incremental, but due to some circumstances, the name of the primary key cannot be just id, so I changed it into budget_id and already declared it at the beginning of the model. Going by the error message, it seems Laravel keeps trying to compare with this id field instead the one I declared. What needs to be done to fix this?
UPDATE IN CODE:
I used Rule namespace on createRules and updateRules on model:
public static function createRules()
{
return [
'budget_code' => ['required', Rule::unique('acq_m_budgets', 'budget_code'), 'max:15'],
'budget_name' => ['required', 'max:100'],
'ma_code' => ['max:10'],
'start_period' => ['required'],
'end_period' => ['required'],
];
}
public static function updateRules($id)
{
return [
'budget_code' => ['required', Rule::unique('acq_m_budgets', 'budget_code')->ignore($id, 'budget_code'), 'max:15'],
'budget_name' => ['required', 'max:100'],
'ma_code' => ['max:10'],
'start_period' => ['required'],
'end_period' => ['required'],
];
}
When I tried to update the data, I made changes to some fields except the budget_code. The changes won't be saved if I didn't change the budget_code field as well, since it always give an error: "budget_code" has already been taken. I use dd($post), and the fields I changed is passed on perfectly.

I would use the Rule namespace, where you can call unique through that. For this to work you have to use arrays, for validation rules instead of strings, this is the better approach for readability anyways.
Rule::unique has the method ignore() where the second parameter is the id column, this can be seen here.
'budget_code' => [
'required',
Rule::unique('acq_m_budgets', 'budget_code')->ignore($id, 'budget_id'),
'max:15'
]

Related

General error: 1364 Field 'title' doesn't have a default value while doing update

Hello I faced the following error:
SQLSTATE[HY000]: General error: 1364 Field 'title' doesn't have a default value.
It happens when I try to update amount of views on specific post.
I've just set default value of points while initializing it specified in the model in $attributes table.
Posts table migration:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->longText('text');
$table->integer('points');
$table->bigInteger('views');
$table->integer('user_id')->unsigned()->nullable();
$table->integer('is_closed');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
});
}
Post model:
const POINTS = 0;
const VIEWS = 0;
const IS_CLOSED = 0;
protected $attributes = [
'points' => self::POINTS,
'views' => self::VIEWS,
'is_closed' => self::IS_CLOSED,
'title' => null,
'text' => null,
'user_id' => null,
];
protected $fillable = [
'title',
'text',
'user_id',
];
My Service where I try to increment the value of views:
public function incrementPostViews($id)
{
$post = $this->post->findOrFail($id);
$post->views++;
return $post->save();
}
I did use the Request rule but only while creating new post:
public function rules()
{
return [
'title' => ['required', 'max:50'],
'text' => ['required', 'max:1000'],
'user_id' => ['numeric', 'nullable'],
];
}
So by default the points, views and is_closed fields are set to 0 while creating new Post. To be honest I do not have any ideas why it is causing an error.
Update:
In Post model I've changed the $attributes array and added title, text and user_id which default value is set to null. Those three fields stays in $fillable array also. I'm not sure if it's the right way to fix it. If it's not, please correct me.
In the migration there are not changes made.
All changes are visible above.
You declared your title item as required in your table but you didn't declare a default value. And your insert operation doesn't give a value for that column, so it fails.
The easiest way to fix this is probably to declare title as nullable instead.
But if you have a good reason to make it required, you'll have to revisit the way you insert your rows so you can offer a value for that column.
You must create THE post first, once that post has been created you can then update its attributes.
$user = Post::create([
'title' => $request->title,
'text' => $request->text,
'points' => 0,
'views' => 0,
'is_closed' => $request->is_closed,
]);

SECURITY TOKEN in first Prestashop 1.7 module

I have tryed now for a few day´s to create a module.
Its going to be a easy one, that add some variable to different table, and show them in a list when you select from a dropdown.
Im only still in admin, the front do i create later,
I follow a tutorials that was easy to follow.
First copy the sql to the db.
CREATE TABLE pasta (
`id` INT NOT NULL AUTO_INCREMENT,
`sku` VARCHAR(255) NOT NULL,
`name` VARCHAR(255) NOT NULL,
`description` TEXT,
`id_pasta_category` INT NOT NULL,
`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
Then i copy the ObjectModel class /override/classes/fc_pasta/Pasta.php
<?php
class Pasta extends ObjectModel {
public $id; // fields are mandatory for create/update
public $sku;
public $name;
public $created;
public $category;
public $id_pasta_category;
public static $definition = [
'table' => 'pasta',
'primary' => 'id',
'fields' => [
'sku' => ['type' => self::TYPE_STRING, 'validate' => 'isAnything', 'required'=>true],
'name' => ['type' => self::TYPE_STRING, 'validate' => 'isAnything', 'required'=>true],
'description' => ['type' => self::TYPE_HTML, 'validate' => 'isAnything',],
'created' => ['type' => self::TYPE_DATE, 'validate' => 'isDateFormat'],
'id_pasta_category' => ['type'=>self::TYPE_INT, 'validate'=>'isUnsignedInt','required'=>true,],
],
];
}
And after that i copy the module /modules/fc_pasta/fc_pasta.php
<?php
if (!defined('_PS_VERSION_')) {exit;}
class Fc_Pasta extends Module {
public function __construct() {
$this->name = 'fc_pasta'; // must match folder & file name
$this->tab = 'administration';
$this->version = '1.0.0';
$this->author = 'Florian Courgey';
$this->bootstrap = true; // use Bootstrap CSS
parent::__construct();
$this->displayName = $this->l('PrestaShop Module by FC');
$this->description = $this->l('Improve your store by [...]');
$this->ps_versions_compliancy = ['min' => '1.7', 'max' => _PS_VERSION_];
// install Tab to register AdminController in the database
$tab = new Tab();
$tab->class_name = 'AdminPasta';
$tab->module = $this->name;
$tab->id_parent = (int)Tab::getIdFromClassName('DEFAULT');
$tab->icon = 'settings_applications';
$languages = Language::getLanguages();
foreach ($languages as $lang) {
$tab->name[$lang['id_lang']] = $this->l('FC Pasta Admin controller');
}
$tab->save();
}
}
And after that i create the AdminPastaController
<?php
require_once _PS_ROOT_DIR_.'/override/classes/fc_pasta/Pasta.php';
class AdminPastaController extends ModuleAdminController {
public function __construct(){
parent::__construct();
// Base
$this->bootstrap = true; // use Bootstrap CSS
$this->table = 'pasta'; // SQL table name, will be prefixed with _DB_PREFIX_
$this->identifier = 'id'; // SQL column to be used as primary key
$this->className = 'Pasta'; // PHP class name
$this->allow_export = true; // allow export in CSV, XLS..
// List records
$this->_defaultOrderBy = 'a.sku'; // the table alias is always `a`
$this->_defaultOrderWay = 'ASC';
$this->_select = 'a.name as `pastaName`, cl.name as `categoryName`';
$this->_join = '
LEFT JOIN `'._DB_PREFIX_.'category` cat ON (cat.id_category=a.id_pasta_category)
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cat.id_category=cl.id_category and cat.id_shop_default=cl.id_shop)';
$this->fields_list = [
'id' => ['title' => 'ID','class' => 'fixed-width-xs'],
'sku' => ['title' => 'SKU'],
'pastaName' => ['title' => 'Name', 'filter_key'=>'a!name'], // filter_key mandatory because "name" is ambiguous for SQL
'categoryName' => ['title' => 'Category', 'filter_key'=>'cl!name'], // filter_key mandatory because JOIN
'created' => ['title' => 'Created','type'=>'datetime'],
];
// Read & update record
$this->addRowAction('details');
$this->addRowAction('edit');
$categories = Category::getCategories($this->context->language->id, $active=true, $order=false); // [0=>[id_category=>X,name=>Y]..]
$categories = [['id'=>1, 'display'=> 'abc'], ['id'=>2, 'display'=>'def']];
$this->fields_form = [
'legend' => [
'title' => 'Pasta',
'icon' => 'icon-list-ul'
],
'input' => [
['type'=>'html','html_content'=>'<div class="alert alert-info">Put here any info content</div>'],
['name'=>'id_xxx','label'=>'XXX','type'=>'select',
'options'=>[ 'query'=>$categories,
'id'=>'id', // use the key id as the <option> value
'name'=> 'display', // use the key display as the <option> title
]
],
['name'=>'name','type'=>'text','label'=>'Name','required'=>true],
['name'=>'description','type'=>'textarea','label'=>'Description',],
['name'=>'created','type'=>'datetime','label'=>'Created',],
['name'=>'id_pasta_category','label'=>'Category','type'=>'select','required'=>true,'class'=>'select2',
'options'=>[ 'query'=>$categories,
'id'=>'id_category', // use the key "id_category" as the <option> value
'name'=> 'name', // use the key "name" as the <option> title
]],
],
'submit' => [
'title' => $this->trans('Save', [], 'Admin.Actions'),
]
];
}
protected function getFromClause() {
return str_replace(_DB_PREFIX_, '', parent::getFromClause());
}
}
Everything almost ok, but everytime i update the page it create a new menu all the time:
FC Pasta Admin controller, instead of 1 i have 25 now.
That I can´t find why.
And one more thing, everything i do in the module i get INVALID SECURITY TOKEN
Im really new to create modules to PS 1.7, but i really whant to try.
You are creating and saving a new backoffice tab in the module constructor,
so a new tab will be created everytime the module object is instantiated.
You'll need to move the tab creation login in the install() method of the module, so that your tab will be created only once during the very first module installation.

Laravel unique rule validator breaking during Validator::make

I am trying to check to see if (a) column(s) is/are unique by using the Rule::unique('table')->where(function($query) use($x) {...}); functionality but when I pass this into my validator I am getting a strange error. What I think is happening is that it is trying to check if a value is equal in the where the statement that I provided but also a column that it THINKS is the unique ID column for the table but it is not so it is breaking.
protected function validator(array $data)
{
$uid = 660000000;
$rule = Rule::unique('member_record')->where(function ($query) use ($uid) {
return $query->where('uniqueID', $uid);
});
return Validator::make($data, [
'fullName' => ['required', 'string', 'min:2'],
'member_id' => [
'bail', 'required', 'Numeric', $rule,
'exists:new_benefits_member,member_id'
],
'email' => ['bail', 'required', 'email', 'confirmed', 'unique:user,email'],
'password' => [
'required', 'string', 'min:8', 'confirmed',
'regex:/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,}$/'
],
'terms' => ['required']
]);
}
However, then I am getting an error that looks like the following.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'member_id' in 'where clause' (SQL: select count(*) as aggregate from member_record where member_id = 660000000 and (uniqueID = 660000000))
What my only assumption is that when I am passing data into the Validator::make($data... it is trying to compare the $rule with the $data array and it is messing it up. Let me know if you have any fixes that I can try out.
The problem here is that the Rule::unique() function can take 2 parameters as shown below
public static function unique($table, $column = 'NULL')
{
return new Rules\Unique($table, $column);
}
if column is left as 'NULL' then this will default to the name of the key in the validator::make($x, [] <--- array
as shown in this example.
protected function validator(array $data)
{
$uid = 660000000;
$rule = Rule::unique('member_record')->where(function ($query) use ($uid) {
return $query->where('uniqueID', $uid)->orwhere('client_member_id', $uid);
});
$data['foo'] = 0;
$validator = Validator::make($data, [
'foo' => [$rule]
]);
return $validator;
}
results in this response
Column not found: 1054 Unknown column 'foo' in 'where clause' (SQL: select count(*) as aggregate from member_record where foo = 0 and (uniqueID = 660000000 or client_member_id = 660000000))
If you would like to exclude "is equal to" in the first part of the where clause you would perform a unique check like this
'member_id' => ['unique:member_record,foo']
If you would like to add additional where clauses then you would want to do something like this
'member_id' => ['unique:member_record,foo,NULL,id,bar,' . $uid]
This will return SQL looking like this
select count(*) as aggregate from member_record where foo = 660000000 and bar = 660000000

Laravel unique validation on multiple columns

I have 2 columns in table servers.
I have columns ip and hostname.
I have validation:
'data.ip' => ['required', 'unique:servers,ip,'.$this->id]
This working only for column ip. But how to do that it would work and for column hostname?
I want validate data.ip with columns ip and hostname.
Because can be duplicates in columns ip and hostname, when user write ip.
You can use Rule::unique to achieve your validation rule
$messages = [
'data.ip.unique' => 'Given ip and hostname are not unique',
];
Validator::make($data, [
'data.ip' => [
'required',
Rule::unique('servers')->where(function ($query) use($ip,$hostname) {
return $query->where('ip', $ip)
->where('hostname', $hostname);
}),
],
],
$messages
);
edit: Fixed message assignation
The following will work on the create
'data.ip' => ['required', 'unique:servers,ip,'.$this->id.',NULL,id,hostname,'.$request->input('hostname')]
and the following for the update
'data.ip' => ['required', 'unique:servers,ip,'.$this->id.','.$request->input('id').',id,hostname,'.$request->input('hostname')]
I'm presuming that id is your primary key in the table. Substitute it for your environment.
The (undocumented) format for the unique rule is:
table[,column[,ignore value[,ignore column[,where column,where value]...]]]
Multiple "where" conditions can be specified, but only equality can be checked. A closure (as in the accepted answer) is needed for any other comparisons.
Laravel 5.6 and above
Validation in the controller
The primary key (in my case) is a combination of two columns (name, guard_name)
I validate their uniqueness by using the Rule class both on create and on update method of my controller (PermissionsController)
PermissionsController.php
<?php
namespace App\Http\Controllers;
use App\Permission;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\Http\Controllers\Controller;
class PermissionsController extends Controller
{
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
request()->validate([
'name' => 'required|max:255',
'guard_name' => [
'required',
Rule::unique('permissions')->where(function ($query) use ($request) {
return $query
->whereName($request->name)
->whereGuardName($request->guard_name);
}),
],
],
[
'guard_name.unique' => __('messages.permission.error.unique', [
'name' => $request->name,
'guard_name' => $request->guard_name
]),
]);
Permission::create($request->all());
flash(__('messages.permission.flash.created'))->success();
return redirect()->route('permission.index');
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Permission $permission)
{
request()->validate([
'name' => 'required|max:255',
'guard_name' => [
'required',
Rule::unique('permissions')->where(function ($query) use ($request, $permission) {
return $query
->whereName($request->name)
->whereGuardName($request->guard_name)
->whereNotIn('id', [$permission->id]);
}),
],
],
[
'guard_name.unique' => __('messages.permission.error.unique', [
'name' => $request->name,
'guard_name' => $request->guard_name
]),
]);
$permission->update($request->all());
flash(__('messages.permission.flash.updated'))->success();
return redirect()->route('permission.index');
}
}
Notice in the update method i added an additional query constraint [ whereNotIn('id', [$permission->id]) ] to ignore the current model.
resources/lang/en/messages.php
<?php
return [
'permission' => [
'error' => [
'unique' => 'The combination [":name", ":guard_name"] already exists',
],
'flash' => [
'updated' => '...',
'created' => '...',
],
]
]
The flash() method is from the laracasts/flash package.
Table
server
Field
id primary key
ip should be unique with hostname
hostname should be unique with ip
Here I validate for Ip and the hostname should be unique.
use Illuminate\Validation\Rule;
$ip = '192.168.0.1';
$host = 'localhost';
While Create
Validator::make($data, [
'ip' => [
'required',
Rule::unique('server')->where(function ($query) use($ip,$host) {
return $query->where('ip', $ip)->where('hostname', $host);
});
],
]);
While Update
Add ignore after RULE
Validator::make($data, [
'ip' => [
'required',
Rule::unique('server')->where(function ($query) use($ip,$host) {
return $query->where('ip', $ip)->where('hostname', $host);
})->ignore($serverid);
],
]);
This works for me for both create and update.
[
'column_1' => 'required|unique:TableName,column_1,' . $this->id . ',id,colum_2,' . $this->column_2
]
Note: tested in Laravel 6.
Try this rule:
'data.ip' => 'required|unique:servers,ip,'.$this>id.'|unique:servers,hostname,'.$this->id
With Form Requests:
In StoreServerRequest (for Create)
public function rules() {
'ip' => [
'required',
Rule::unique('server')->where(function ($query) {
$query->where('ip', $this->ip)
->where('hostname', $this->host);
})
],
}
public function messages() {
return [
'ip.unique' => 'Combination of IP & Hostname is not unique',
];
}
In UpdateServerRequest (for Update)
Just Add ignore at the end
public function rules() {
'ip' => [
'required',
Rule::unique('server')->where(function ($query) {
$query->where('ip', $this->ip)
->where('hostname', $this->host);
})->ignore($this->server->id)
],
}
This is the demo code. It would help you much better. I tried covering both insert and update scenarios.
Inside app/Http/Providers/AppServiceProvider.php
Validator::extend('uniqueOfMultiple', function ($attribute, $value, $parameters, $validator)
{
$whereData = [
[$attribute, $value]
];
foreach ($parameters as $key => $parameter) {
//At 0th index, we have table name
if(!$key) continue;
$arr = explode('-', $parameter);
if($arr[0] == 'except') {
$column = $arr[1];
$data = $arr[2];
$whereData[] = [$column, '<>', $data];
} else {
$column = $arr[0];
$data = $arr[1];
$whereData[] = [$column, $data];
}
}
$count = DB::table($parameters[0])->where($whereData)->count();
return $count === 0;
});
Inside app/Http/Requests/Something/StoreSometing.php
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'name' => 'required|max:225|uniqueOfMultiple:menus,location_id-' . $this->get('location_id', 'NULL') . ',language_id-' . $this->get('language_id', 1),
'location_id' => 'required|exists:menu_location,id',
'order' => 'digits_between:0,10'
];
}
Inside app/Http/Requests/Something/UpdateSomething.php
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'name' => 'required|max:225|uniqueOfMultiple:menus,location_id-' . $this->get('location_id', 'NULL') . ',language_id-' . $this->get('language_id', 'NULL') . ',except-id-' . $this->route('id', 'NULL'),
'location_id' => 'required|exists:menu_location,id',
'order' => 'digits_between:0,10'
];
}
Inside resources/lang/en/validation.php
'unique_of_multiple' => 'The :attribute has already been taken under it\'s parent.',
Here in this code, the custom validation used is uniqueOfMultiple. The first argument passed is the table_name i.e menus and all other arguments are column_name and are comma-separated. The columns are used here, name (primary column), location_id, language_id and one except-for column for the update case, except-id. The value passed for all three is - separated.
This works for me for both create and update.
in your ServerUpdateRequest or ServerCreateRequest class
public function rules()
{
return [
'column_1' => 'required|unique:TableName,column_1,' . $this->id . ',id,colum_2,' . $this->column_2 . ',colum_3,' . $this->column_3,
];
}
This command run background a aggregate Sql like this
select
count(*) as aggregate
from
`TableName`
where
`column_1` = <postedColumn1Value>
and `id` <> idValue
and `column_2` = <postedColumn2Value>
and `column_3` = <postedColumn3Value>
tested in Laravel 9. and it works
Note: if you want to see background sql for debugging (For example, to check if the request values are empty[$this->]) , especially you have to write wrong code, For example, you may enter a filed name incorrectly.
for me laravel 8 this works
$req->validate([
'house_no' => [
Rule::unique('house')
->where('house_no', $req->input('house_no'))
->where('ward_no', $req->input('ward_no'))
],
]);
The following code worked nicely for me at Laravel 8
Create:
'required|unique:TableName,column_1,' . $this->column_1 . ',id,colum_2,' . $this->column_2,
Example:
public function store(Request $request)
{
$union = auth()->user()->union_id;
$request->validate([
'holding_no' => 'required|integer|unique:holding_taxes,holding_no,' . $request->holding_no . ',id,union_id,' . $union,
]);
}
Update:
'required|unique:TableName,column_1,' . $this->id . ',id,colum_2,' . $this->column_2,
Example:
public function update(Request $request, $id)
{
$union = auth()->user()->union_id;
$request->validate([
'holding_no' => 'required|unique:holding_taxes,holding_no,' . $id . ',id,union_id,'.$union,
]);
}
Simple solution with call back query
Rule::unique('users')->where(fn ($query) => $query->where(['project_id'=> request()->project_id, 'code'=> request()->code ])),
public function store(Request $request)
{
$this->validate($request, [
'first_name' => 'required|regex:/^[\pL\s\-]+$/u|max:255|unique:contacts,first_name, NULL,id,first_name,'.$request->input('last_name','id'),
'last_name'=>'required|regex:/^[\pL\s\-]+$/u|max:255|unique:contacts,last_name',
'email' => 'required|email|max:255|unique:contacts,email',
'job_title'=>'required',
'city'=>'required',
'country'=>'required'],
[
'first_name.regex'=>'Use Alphabets Only',
'email.unique'=>'Email is Already Taken.Use Another Email',
'last_name.unique'=>'Contact Already Exist!. Try Again.',
]
);

How to pass a variable to custom validator

I've created a custom validator that resides in the AppServiceProvider. The boot method contains a DB method that should accept the first patameter passed into the validator as the table name. When I manually fill out the table name, it works but when the first parameter is passed, I run into this error:
QueryException in Connection.php line 729:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'server1.{users}'
doesn't exist (SQL: select count(*) as aggregate from `{users}` where `email` =
mail#mail.com and `STORE_ID` = 2)
Here's my service provider code:
public function boot()
{
Validator::extend('uniqueForStore', function ($attribute, $value, $parameters, $validator) {
$count = DB::table($parameters[0])->where($attribute, $value)->where('STORE_ID', config('constants.STORE_ID'))->count();
return $count === 0;
});
}
This is where the problem lies:
DB::table($parameters[0])
Here's my register user form request code:
public function rules()
{
return [
'first_name' => 'required',
'last_name' => 'required',
'email' => "uniqueForStore:{users}",
'password' => 'required|min:6'
];
}
Set your validation rules as below - just remove brackets for unique value (users):
public function rules()
{
return [
'first_name' => 'required',
'last_name' => 'required',
'email' => "uniqueForStore:users",
'password' => 'required|min:6'
];
}

Categories