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);
}),
],
]);
Related
Am following a tutorial attempting to learn how to use Laravel5.5. After following a step which said to add protected $fillable = array('email', 'password', 'name'); to the User model.
Which I'm assuming is setting which fields are mass assignable fields? The next step was to add:
User::create(array(
'email' => 'your#email.com',
'password' => Hash::make('password'),
'name' => 'John Doe'
));
Which I'm to understand is to add said user to the db.
When I run php artisan migrate I receive [Illuminate\Database\Eloquent\MassAssignmentException] email and I have no idea why. I have tried adding Eloquent::unguard(), have tried to make it all guardable.
Have removed email from the fillable array. Have got rid of email altogther.
Each time I run php artisan migrate the error is this same.
It should work without any problems based on your description.
So make sure:
you are using valid User model (maybe you have 2 and you set fillable only in one of them)?
you are not inserting other data in migrations. It's quite possible the problem is for other model and not for User model.
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.
I have this where i have all features from database:
$features = Feature::all();
User can add new additional feature that will be added in this table, but i want to validate so if user enter something that is already in databse to get a message. So name need to be unique. Any suggestion how can i do that?
I tried this but it save it anyway.
$this->validate($request, [
'name' => 'unique:features',
]);
From the docs
unique:table,column,except,idColumn
The field under validation must be unique in a given database table. If the column option is not specified, the field name will be used.
Specifying A Custom Column Name:
'email' => 'unique:users,email_address'
You may need to specify the column to be checked against.
$feauturescheck= Feauture::where('Columname', '=',Input::get('input'))->count();
After 2 years experience in cakePhp , recently i started learning laravel framework. i already created the database for my new project and create a default authentication system with laravel 5 using the
php artisan make:auth
but i cant modify it according to my database design
i have a
users table(id,username,password,remember_token)
user_profiles table (id,first_name,last_name etc)
group table(id,group_name)
group_users(group_id,users_id)
users_user_profiles (user_id,user_profile_id)
so using the above table i want to implement a group based user management system.
if i use the default authentication system ,
how can i store login and registration details into two tables with a single registration from? and how can i insert the id's into pivot table users_user_profiles table automatically ?
How to create a view for the registration which contain both the login details and registration details, how can i separate them and how to access the request data separately for storing into two separate tables?
if anybody can provide a detailed description on this, it will help all laravel 5 beginners, i think its a common problem for all newbies..
please help me
Thanks
First define good your models, so you can use eloquent, for example making connection with user_profiles:
public function profiles()
{
return $this->belongsToMany('UserProfile');
}
(It will be possible only when you get UserProfile model)
Then you extend your register form by adding first_name, last_name, etc. In auth controller after creating user
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
Add code that create user profile:
$userProfile = UserProfile::create([
'first_name' => $data['name'],
'last_name' => $data['email']
]);
Then add it to user by attach command (if you have many of them you can use sync) :
$user->userProfiles()->attach($userProfile->id);
More info about how user Authentication work you can find exterminating file: \vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php
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.