i have problem validating a bulk insertion in laravel 5 the scenario that i have is as following :
a model called department that has many employees when i save a department it may have several employee belongs to it , I'm currently loops the entire employee list and validate each one before insertion is there a way in laravel 5 validator that do this out of the box.
In Laravel 5.2 there's an easy way of taking care of this kind of problems.
Validating array form input fields is much easier in Laravel 5.2. For example, to validate that each e-mail in a given array input field is unique, you may do the following:
$validator = Validator::make($request->all(), [
'person.*.email' => 'email|unique:users'
]);
Likewise, you may use the * character when specifying your validation messages in your language files, making it a breeze to use a single validation message for array based fields:
'custom' => [
'person.*.email' => [
'unique' => 'Each person must have a unique e-mail address',
]
],
This info is clearly explained in the docs.
Source: laravel.com
To my knowledge there is not a way to do this in Laravel out of the box. By that I mean there is no method you can call or class you can use that will accomplish your goal.
You can create one though. Abstract the validation out into it's own class and use that.
Related
Currently the system has a few dozen models, controllers and a few hundred routes.
When carrying out any query in the database, if a certain value is found during that query, I can transform this data into another value.
An example to facilitate understanding is, when performing the query and before presenting the result, a hashtag is found, that hashtag is replaced with another value.
In this example, the difficulty is not to change the value itself (str_replace()), but to be able to intercept any of the results of queries to the database, search for this "keyword" and replace it.
But this change is only visual, it is not replacing the data in the database.
Of course I can do this on each controller, but due to the quantity, I don't think anything is viable
I think I need to somehow be able to intercept all the results of any consultation with the database and make this substitution, but I have no idea if I should use Middleware or another Laravel resource, or even that should be done by a ServiceProvider.
You can achieve this using Laravel's Resources.
You could create a HashtagResource and run all model results through it prior to utilising the data which could then look for the hashtag and replace it within the content as described.
For example, inside of the newly generated resource that wraps around your query, ie HashtagResource::collection(MyModel::all())
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'content' => str_replace('#hashtag','#otherhashtag', $this->content)
];
}
I have some questions regarding having CakePHP 3 sessions table stored in the database:
1) Is there a way to remname the table from sessions to a different name? If yes, where should I specify the new name?
2) Similarly to question 1: Is there a way to remname the names of colums in sessions table, so that CakePHP would still operate correctly?
3) Is there a simple way to add even the most basic encription of the data column of sessions table?
So I will cite the database session docs and then also provide some answers. The answers in the docs are more complete.
Specify the model you want to use by including a 'model' key.
'Session' => [
'defaults' => 'database',
'handler' => [
'engine' => 'DatabaseSession',
'model' => 'MyCustomSessions'
]
]
The DatabaseSession handler appears to hard code 'data' and some of the other columns. However, you can (and should) extend that class if you want to do something special
Based on what you want to do it appears creating your own SessionHandler is the way to go. It is quite simple to do and it is outlined in the docs.
My best advice would to take a peek at cake's DatabaseSession. It seems that your requirements are special and it is probably your best course of action.
is there an available validation rules that lets you compare the input field and the column in the database if they match
like they should match the user id and the session and the inputted password and the password in the database?
You're looking for exists rule:
The field under validation must exist on a given database table.
'form_field' => 'exists:table,column'
Stumbled upon this while I was looking for the same answer as well. This maybe a bit old, but as Christian mentioned in the comments, the solution by Alexey allows the user to use any existing password (even another user's) in the database as the current password. Hope this answer helps someone :)
So for Laravel 5.2 the below solution worked for me as mentioned in the documentation:
'old_password' => 'required|exists:users,password,usr_id,'.$user_id,
This maybe different in Laravel 5.3+, as the Laravel 5.3 documentation has the following validation for customizing the query. I haven't tried this, but it does not work for Laravel 5.2.
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::exists('staff')->where(function ($query) {
$query->where('account_id', 1);
}),
],
]);
I have a model for articles, the article can be written in two languages, English and Arabic.
So there are 2 attributes for each language text, I want the both attributes to be allowed to be empty on create and update, knowing that the attributes type are text not varchar.
How i can write a rule for what i need?
I am not sure what you mean by "..knowing that the attributes type are text not varchar..." but Yii has built in mechanism to define your validation rules.
Refer to the Yii documentation for more details.
#hamed pointed out in his answer how to make fields mandatory. You can add to this by specifying the scenarios under which the validation rules are true. For example.
array('field1, field2', 'required', 'on' => array('create', 'update'));
will apply the 'required' rule only for inserting and updating. On the other hand.
array('field1, field2', 'required', 'except' => array('create', 'update'));
will apply the 'required' rule only for all scenarios except inserting and updating.
http://www.yiiframework.com/doc/api/1.1/CStringValidator
array(
array('text_en','length'),
array('text_ar','length'),
);
When you generate a model from a database table, yii adds something like this inside rule() function:
array('field1, field2, ...', 'required')
This array is based on NOT NULL attribute in the database column. It means, if the database field has NOT NULL property checked, yiis add it in the above array. All you need to do is removing your two fields from above array.
I have a form that accepts data which will be used to create two new database table entries. The form takes both the users details and their address. The user details will be stored using the User::create(Input::all()) method into the users table, and the address details will be stored using the Address::create(Input::all()) method into the addresses table of the database.
The issue I'm currently having is that Eloquent is complaining that street, city, country etc do not exist on the users table. This is true, that data is to be used for the address side of things.
Is there any way to have eloquent ignore irrelevant data in the Input::all() array when it's passed to the create methods?
P.s. I'm aware that mass-assignment isn't a good idea, I'm only using it here to simplify my question.
Sure enough you can use $fillable array in your model to declare fields allowed for mass-assignment. I believe this is the most sufficient solution in your case.
class User extends Eloquent {
protected $fillable = [
'first_name',
'last_name',
'email'
];
}
Have you tried looking at Input::only('field1','field2',...);, or even Input::except('field3')? They should be able to accomplish what you are looking for.
Source: http://laravel.com/docs/requests
You'll have to unguard that model using these http://laravel.com/docs/eloquent#mass-assignment and then manually unset those values before you execute save(). I highly recommend using a form object or something similar to complete this kind of service for you outside of your model since it's safer and usually clearer to intended behavior.
#cheelahim is correct, When passing an array to Model::create(), all extra values that aren't in Model::fillable will be ignored.
I would however, STRONGLY RECOMMEND that you do not pass Input::all() to a model. You really should be validating and verifying the data before throwing it into a model.