Confusing Laravel Validation - php

can someone explain this laravel validation
'email' => 'unique:users,email_address,NULL,id,account_id,1'
What's the purpose of NULL, id in it?

Basic Usage Of Unique Rule
'email' => 'unique:users'
Specifying A Custom Column Name
'email' => 'unique:users,email_address'
Forcing A Unique Rule To Ignore A Given ID
'email' => 'unique:users,email_address,10'
Adding Additional Where Clauses
You may also specify more conditions that will be added as "where" clauses to the query:
'email' => 'unique:users,email_address,NULL,id,account_id,1'
In the rule above, only rows with an account_id of 1 would be included in the unique check.
Documents References (i.e. https://laravel.com/docs/5.0/validation)

Related

Elasticsearch search on different index with different field of same cluster

I need help to search in the different indexes of the same cluster with different fields. For example: If I have a cluster name demo in which I have 2 indexes menu_items and purchase_orders. Both indexes contain different columns to search. Index menu_items contains 'label' field and index purchase_orders contains 'id' and 'supplier_reference' field.
$params = [
'index' => 'menu_items,purchase_orders',
'_source' => 'label,id,supplier_reference',
'body' => [
'query' => [
'bool' => [
'must' => [
'multi_match' => [
'query' => $keyword,
'type' => 'bool_prefix',
'fields' => ['label', 'id', 'supplier_reference'],
],
],
],
],
],
];
Now If I am searching for any term '3312' which is available in both indexes so this query working good and another term 'supplier' is not available in another index at that time I got an error.
Can anyone help here? How can I achieve this? How can we find fields in two or more different indexes?
I face an issue as described below and as a solution what I found here is, we need to do the proper mapping.
Related to mapping, Elasticsearch has the built in ability to detect the type of the fields of a document and generate a schema and apply it throughout the index. But this causes an error while two indexes have different data types for the same field. ex: if index1 has id field with long data type and index2 has id field with the string data type. At that time search operation gives an error for search related to the id field which I described in the question.

laravel 5.4 validate array with custom rule

I'm having trouble validating an array of fields that requires a custom rule. I have the following validator:
$validator = Validator::make($request->all(), [
'order' => 'required',
'service_id.*' => Rule::unique('order_services')->where('order_id', $request->order),
'due_date.*' => 'required|date',
'vendor' => 'required|integer',
'instructions' => 'string|nullable',
'lock_box' => 'string|nullable',
]);
The due date array validates just fine, but the service id returns the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'service_id.0' in 'where clause' (SQL: select count(*) as aggregate from `order_services` where `service_id`.`0` = 10 and `order_id` = 100f)
The service id rule prevents duplicate records with the same order number and service id
The rule works as expected when validating a single service ID, I'm just not sure how to validate multiple service IDs at the same time.
Thanks in advance
By default Laravel uses key for unique role and because it's an array, keys are 0, 1, 2 and so on. The solution is adding column name for unique rule like this:
Rule::unique('order_services', 'id')->where('order_id', $request->order),
Obviously in your case you might need other column name than id (it depends on your application)

How to insert Array to Database using firstOrCreate in Laravel?

I have an array like this and it has 120 elements in it
`array (size=120)
0 =>
array (size=8)
'name' => That the quick brown fox jumps over the lazy - 7' (length=53)
'url' => string 'google.com/zyx' (length=134)
'category' => string 'search-engine' (length=6)
1 =>
array (size=8)
'name' => string 'Mr. john brandy gave me a wall nut of quite' (length=67)
'url' => string 'yahoo.com/dzxser' (length=166)
'category' => string 'indian' (length=6)`
I want to insert them to my bookmark table which model I have created and I want to make sure duplication doesn't occur. I have found this https://laravel.com/docs/5.4/eloquent#other-creation-methods specially firstOrCreate method.
I assume I have to use foreach but I am not sure how. Can anyone help me with some workaround.
Actually you don't need firstOrCreate, you need updateOrCreate. Checking Laravel Other Creation methods You will find that method.
Say that array is in $alldata:
foreach($alldata as $data) {
MyModel::updateOrCreate($data); //the fields must be fillable in the model
}
This will run update/or create of 120 queries while cycling through the loop. The advantage is that, you cannot have a duplicate, rather if there is a repetition, its only going to perform an update to the table.
However the best way to ensure that there is no duplication in whatever way the data comes is to set it up when making your database table. You can set unique constraints on many fields if thats your case.
If you don't want duplication to occur when inserting array of records then all you have to do it set a constraint making sure fields are unique.
If you're using migrations to create databse schema you can use something like this: $table->string('name')->unique();
Now for example, this will make sure that 'name' column data is

CakePHP Smart Column names

When creating tables for a CakePHP 2.X application there are certain column names that Cake will automatically process in special ways. The most obvious I can think of would be "created" and "modified" (which Cake will automatically handle when rows are created or edited). There's "name" which will by default be used as a model's display name. To a lesser degree "email" and "phone" automatically select their appropriate validation rules when Baking the project.
My Question is:
Does anyone have a comprehensive list of these column names?
I've tried looking through the CakePHP site and Google searches, but either this list does not exist, or I'm using the wrong the terms.
Thank you.
For the forms, if the field name is:
'pasword', 'passwd', 'password' => it will be displayed as password fields
'tel', 'telephone', 'phone' => it will be displayed a type = "tel"
'text' => textarea
'time' => time drop-downs
'datetime', 'timestamp' => date + time drop-downs
'date' => date drop-downs
'created', 'updated', 'modified' are automatically filled when using model->save()
You also mentioned "name" which is a model attribute. The list of attributes is here
You can see more about how the fields are displayed in forms in the FormHelper.php and more about how the model works in Model.php from Cake core
Regarding cake bake validations:
if field name is email, it will be validated as email.
The other validations are based on field type:
type string => validation: notEmpty
type integer => validation: numeric
type float => validation: numeric
type boolean => validation: boolean
type date => validation: date
type time => validation: time
type datetime => validation: datetime
type inet => validation: ip

conditions in contain cakephp find

is it good or bad practice in CakePHP to have conditions set in the contain of a find query like:
$data = $this->SomeModel->find('all', array(
'contain' => array(
'AnotherModel' => array(
'conditions' => array(
// some conditions
)
)
)
));
In which cases will putting conditions inside a contain be useful, and when should I use it or not. Sorry, this is still confusing to me.
Thank you
I'm not sure whether this is a good idea... First of all, I'm going to assume that this specific case requires you to specify conditions that unique to this case, i.e. not general enough for you to put into your SomeModel model relationship criteria to AnotherModel.
My suggestions would be that you should put those conditions in your overall find conditions, as contain specifies which linked models to return (controlling the joins under the hood). Like in SQL, you can join another table and specify which records to match in your WHERE clause.
From the manual, you can specify conditions in your contain, but they won't affect the results that don't join your model like the overall conditions will.
I'd do this:
$data = $this->SomeModel->find('all', array(
'contain' => array('AnotherModel'),
'conditions' => array(
// some conditions relating to AnotherModel
)
));

Categories