Here is my code :
$this->validate($req, [
'city' => 'sometimes|unique:regions,city,NULL,id,deleted_at,NULL',
'province' =>'required_without:city|unique:regions,province,NULL,id,deleted_at,NULL,city,NULL',
]);
I want
1.province required and unique when city null.
2.province should not unique when city not_null
3.table name regions
How to achieve?
I'm guessing at your table name and columns here, but this should be close:
'province' => 'required_without:city|unique:provinces,name',
Changes provinces to the correct table name and name to the column you store them under.
Related
Hello i am trying to make a validation that checks the inserted values if they exist or not in other tables. This is the code i have done now:
$request->validate([
'order_number' => 'required',
'client_id' => 'required,exists:clients',
'description' => 'required',
]);
These values are inserted in a table called order but the client_id is taken from another table called clients and i want to verify if the value of client_id exists in the row id of the table clients
Please check here
Laravel Validation
As mentioned in laravel document you need to specify a column name, because if you don't, it will use the key in the request for the column name, so in your example, it will be the clients table and client_id column but you need to specify the column name in exists rule, it will be something like this:
$request->validate([
'order_number' => 'required',
'client_id' => 'required|exists:clients,id',
'description' => 'required',
]);
Also, you used the wrong syntax for separating rules, after required you need to use a pipe(|) to separate your rules and use multiple validations.
I have 3 tables below with relationship applied accordingly.
Order Table - (order_id, stock_id) - Need to have stock id, in order to assign available stock to the order.
Stock Table - (stock_id, product_id, order_id) - have product id in order to know the product name.
Product Table - (product_id, name)
When I want to create order, I want to show the available stock to assign for my order.
Below code is able to show me the available stock with its product_id, however, how can I link it to show product name in this case? Is there any specific relationship method or attribute I can use to achieve my objective?
$this->crud->addField([
'name' => 'stock_id',
'type' => 'select2',
'label' => 'Stock',
'entity' => 'stock',
'attribute' => 'product_id',
'model' => 'App\Models\Stock',
'options' => (function ($query) {
return $query->orderBy('created_at', 'DESC')->where('order_id', null)->get();
}),
]);
i have a request for array of values and i validate it with the following code
$request->validate([
'doctor_id.*' => ['required'],
'doctor_id' => [Rule::unique('project_orders')->where(function ($query) use ($student) {
$query->where('student_id', $student->id);
})],
]);
but the unique validation doesn't work and the data was inserted to the table with duplication
i want the doctor_id field to be unique with the student_id column, what should be the correct rule?
any help please ?
First you need to do validation on database level, so you will add in your migration
$table->unique(['doctor_id', 'student_id']);
This will make sure that there will be no duplication for same doctor_id and student_id values
Then in your validation layer you will add
$request->validate([
'doctor_id.*' => 'unique:project_orders,doctor_id,NULL,id,student_id,'.$student->id,
]);
You can do the validation like this
$request->validate([
'doctor_id.*' => [ Rule::unique('project_orders', 'doctor_id')->where('student_id', $student->id) ]
]);
I am creating a CakePHP application which have an edit form which contain one text box named air_id. In my table I am using project_id and air_id as composite primary key. So while updating air_id I need to validate uniqueness.
My table structure is like:
project_id air_id
1 test#test.com
1 test1#test.com
Currently I am using cakephp3.0 and I am using validateUnique rule with scope,
Following is my code:
$validator
->add('air_id', [
'unique' => [
'rule' => ['validateUnique', ['scope' => 'project_id']],
'provider' => 'table',
]
]);
And my controller is like this
$projectCustomers = $this->ProjectCustomers->newEntity($formData);
Now it is giving validation message every time. What I need is when I change the value test#test.com to test1#test.com it should raise the error and if I change it to some other value it should not raise the error. Is there something wrong in my validation?
CakePHP Unique Field Rules:
We have cakephp unique field rules that might be even better:
In your table(eg. UsersTable.php):
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(
['air_id', 'project_id'],
'Your validation error here.'
));
return $rules;
}
At the top of your table , don't forget to include this class:
use Cake\ORM\RulesChecker;
See Here (CakePHP Unique Fields Rules).
Can I add a part of the fields of an table to the index of algolia?
For example:
There is a table articles,it's fileds like this:
id title content author status created_at updated_at deleted_at
I only want to add the 4 fileds id title content author to the index of algolia,
I read the docs of Scout in Laravel 5.3,it seems not to be mentioned.
You need to implement the toSearchableArray method on your model.
public function toSearchableArray()
{
return [
'objectID' => $this->id,
'title' => $this->title,
'content' => $this->content,
'author' => $this->author,
]
}