These credentials do not match our records - laravel

I have an app with laravel in which, I have different users and different roles. I insert some users, roles, role_user, permission and permission_role in my DB using seeder but, when I try to login using email and password already recorded in DB, I got : These credentials do not match our records.
this is UserTableSeeder :
public function run()
{
//
$user = [
[
'name' => 'admin',
'email' => 'admin#mail.co',
'password' => 'passwordadmin'
],
[
'name' => 'president',
'email' => 'President#mail.co',
'password' => 'passwordpresident'
],
[
'name' => 'utilisateur1',
'email' => 'utilisateur1#mail.co',
'password' => 'passworduser'
],
[
'name' => 'utilisateur2',
'email' => 'utilisateur2#mail.co',
'password' => 'passworduser'
]
];
foreach ($user as $key => $value) {
User::create($value);
}
}
RoleTableSeeder :
public function run()
{
//
$role = [
[
'name' => 'admin',
'display_name' => 'Administrateur',
'description' => 'Administrateur du système'
],
[
'name' => 'president',
'display_name' => 'Président',
'description' => 'President de la commune'
],
[
'name' => 'utilisateur_normal',
'display_name' => 'membre du conseil',
'description' => 'membre du conseil'
]
];
foreach ($role as $key => $value) {
Role::create($value);
}
}
RoleUserTableSeeder :
public function run()
{
//
DB::table( 'role_user' )->insert([
[ 'user_id' => 6, 'role_id' => 4 ],
[ 'user_id' => 7, 'role_id' => 5 ],
[ 'user_id' => 8, 'role_id' => 6 ],
[ 'user_id' => 9 , 'role_id' => 6 ],
]);
}
Models I have : User, Role, Permission.
Any idea please ?

It looks you are not hashing your passwords into your database, you need to use bycript:
public function run()
{
//
$user = [
[
'name' => 'admin',
'email' => 'admin#mail.co',
'password' => bcrypt('passwordadmin')
],
[
'name' => 'president',
'email' => 'President#mail.co',
'password' => bcrypt('passwordpresident')
],
[
'name' => 'utilisateur1',
'email' => 'utilisateur1#mail.co',
'password' => bcrypt('passworduser')
],
[
'name' => 'utilisateur2',
'email' => 'utilisateur2#mail.co',
'password' => bcrypt('passworduser')
]
];
foreach ($user as $key => $value) {
User::create($value);
}
}
You can see more seeder examples on the oficial seeder Laravel documentation.

As Troyer said, you need to hash them with bcrypt. Laravel provides a handy wrapper with \Hash::make($str).
It is extremely bad practice to have your passwords in plaintext.
You need to run your seeder like this:
public function run()
{
$users = [
[
'name' => 'admin',
'email' => 'admin#mail.co',
'password' => \Hash::make('passwordadmin') // Hash them in order to make use of \Auth library (and more importantly, in order not to be a dick to your users)
],
[
'name' => 'president',
'email' => 'President#mail.co',
'password' => \Hash::make('passwordpresident')
],
[
'name' => 'utilisateur1',
'email' => 'utilisateur1#mail.co',
'password' => \Hash::make('passworduser')
],
[
'name' => 'utilisateur2',
'email' => 'utilisateur2#mail.co',
'password' => \Hash::make('passworduser')
]
];
User::create($users); // Import them with 1 query for performance
}

Related

broken column sequence backpack laravel

There was a column order issue. I finished making edits, after a few hours I returned to the project and saw that the columns of the table were mixed up, although before that everything worked correctly.
This is my setup method
public function setup()
{
/*
|--------------------------------------------------------------------------
| CrudPanel Basic Information
|--------------------------------------------------------------------------
*/
$this->crud->setModel('App\Models\Geozone');
$this->crud->setRoute(config('backpack.base.route_prefix') . '/geozone');
/*
|--------------------------------------------------------------------------
| CrudPanel Configuration
|--------------------------------------------------------------------------
*/
$this->crud->allowAccess('show');
$this->crud->setActionsColumnPriority(10000);
$this->transIt();
$this->addColumnsT([
[
'name' => "polygons_json",
'visibleInTable' => false,
],
[
'name' => 'groups', // the method that defines the relationship in your Model
'type' => "select_multiple",
'entity' => 'groups', // the method that defines the relationship in your Model
'attribute' => "description", // foreign key attribute that is shown to user
'model' => "App\Models\GeozoneGroup", // foreign key model
//'visibleInTable' => false,
'number_columns' => 1,
],
[
'name' => "priority",
'visibleInTable' => false,
],
[
'name' => 'uz',
'type' => 'check',
],
[
'name' => 'frz',
'type' => 'check',
],
[
'name' => 'slz',
'type' => 'check',
],
[
'name' => 'ntz',
'type' => 'check',
]
]);
if(\Request::has('filter')) $this->crud->query = $this->crud->query->where(\Request::get('filter'), true);
if(\Request::has('deleted')){
$this->crud->addClause('onlyTrashed');
$this->crud->removeAllButtonsFromStack('line');
$this->crud->addButtonFromModelFunction('line', 'restore', 'restoreActionButton');
}
$this->addFieldsT([
[
'type' => "textarea",
'name' => 'polygons_json',
'attributes' => [
'readonly'=>'readonly',
],
],
]);
$this->addFieldsT([
[
'type' => "checklist",
'name' => 'groups', // the method that defines the relationship in your Model
'entity' => 'groups', // the method that defines the relationship in your Model
'attribute' => "description", // foreign key attribute that is shown to user
'model' => "App\Models\GeozoneGroup", // foreign key model
'pivot' => true,
'direction' => 'row'
],
]);
$this->addFieldsT([
[
'type' => "color_picker",
'name' => 'fill_color',
'attributes' => [
'pattern' => '^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$'
]
],
]);
$this->addFieldsT([
[
'type' => "color_picker",
'name' => 'stroke_color',
'attributes' => [
'pattern' => '^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$'
]
],
]);
$this->addFieldsT([
[
'name' => 'fill_opacity',
'attributes' => [
'pattern' => '\d{1}(\.\d{1,2})?'
]
],
]);
$this->addFieldsT([
[
'name' => 'stroke_opacity',
'attributes' => [
'pattern' => '\d{1}(\.\d{1,2})?'
]
],
]);
$this->crud->addFilter([ // select2_multiple filter
'type' => 'select2_multiple',
'name' => 'car_group',
'label'=> trans('columns.groups'),
], function() { // the options that show up in the select2
return \App\Models\GeozoneGroup::all()->pluck('description','id')->toArray();
}, function($values) {
$this->crud->addClause('join', 'geozone_to_group', 'id', 'geozone_to_group.geozone_id');
$this->crud->addClause('wherein', 'geozone_to_group.geozone_group_id', json_decode($values));
});
$this->crud->setCreateView('geozone_create');
$this->crud->setEditView('geozone_edit');
// add asterisk for fields that are required in GeozoneRequest
$this->crud->setRequiredFields(StoreRequest::class, 'create');
$this->crud->setRequiredFields(UpdateRequest::class, 'edit');
}
So the columns were arranged
[![enter image description here](https://i.stack.imgur.com/kQ1rP.jpg)](https://i.stack.imgur.com/kQ1rP.jpg)
I tried to change their location in the addColumns method, but it doesn't work. Although it works in other controllers.

Create a collection given a number of times keeping the key

I'm trying to create a kind of dataset using laravel Collections, for this I'm using the "times" method:
function getValidData(int $amount): Collection
{
return Collection::times($amount, function (int $number): array {
$password = faker()->password(8, 12);
return [
"User #{$number}" => [
'name' => faker()->firstName(),
'email' => faker()->unique()->safeEmail(),
'password' => $password,
'password_confirmation' => $password
]
];
});
}
However, the result is not as I expected:
[
0 => [
'User #1' => [
'name' => 'ana',
'email' => 'ana#email.com',
'password' => '12345678',
'password_confirmation' => '12345678'
]
],
1 => [
'User #2' => [
'name' => 'leo',
'email' => 'leo#email.com',
'password' => '3231232132',
'password_confirmation' => '3231232132'
]
]
]
I was hoping it would be:
[
'User #1' => [
'name' => 'ana',
'email' => 'ana#email.com',
'password' => '12345678',
'password_confirmation' => '12345678'
],
'User #2' => [
'name' => 'leo',
'email' => 'leo#email.com',
'password' => '3231232132',
'password_confirmation' => '3231232132'
]
]
I can solve this using array_merge(...(Collection::times...)) but it seems a kinda off... Is there any way to solve this in a cleaner way using Collections?
If you would like to map the collection with new keys, you could do the following, using the mapWithKeys() function.
Example
$users = collect(range(1, $amount))
->mapWithKeys( function($index) {
$password = faker()->password(8, 12);
return [sprintf("User #%d", $index) => [
'name' => faker()->firstName(),
'email' => faker()->unique()->safeEmail(),
'password' => $password,
'password_confirmation' => $password,
]];
});
This will run from 1 to the specified $amount. Example output with 3 users:
Illuminate\Support\Collection {#2016
#items: array:3 [
"User #1" => array:4 [
"name" => "Garret"
"email" => "aurelio03#example.com"
"password" => "Vp=Et3!?w0"
"password_confirmation" => "Vp=Et3!?w0"
]
"User #2" => array:4 [
"name" => "Genoveva"
"email" => "hilda.bins#example.net"
"password" => "W?miL1Kts"
"password_confirmation" => "W?miL1Kts"
]
"User #3" => array:4 [
"name" => "Zella"
"email" => "mmurray#example.net"
"password" => "g8)"pYgjQ~"
"password_confirmation" => "g8)"pYgjQ~"
]
]
}
You could also chain the toArray() method on the collection if you prefer an Array over a Collection.

Proper formatting of a JSON and multiplepart Guzzle post request

I have two different Guzzle post requests that I am trying to merge (solely because they basically do a united job and should be performed together).
Initially I have my donation data:
'donation' => [
'web_id' => $donation->web_id,
'amount' => $donation->amount,
'type' => $donation->type,
'date' => $donation->date->format('Y-m-d'),
'collection_id' => NULL,
'status_id' => $donation->status_id,
],
And then I have my files that go with it, which are basically two different PDFs that are enabled or disabled for donors, sometimes they have both. I know the multipart would look something like below, but I'm not sure.
foreach ($uploadDocs as $doc) {
'multipart' => [
[
'name' => 'donation_id',
'contents' => $donation->web_id,
],
[
'name' => 'type_id',
'contents' => $doc->type_id',
],
[
'name' => 'file',
'contents' => fopen($doc->path, 'r'),
'headers' => ['Content-Type' => 'application/pdf'],
],
],
}
Since I've usually only handled one file at a time and I'm not sure how to merge the first block of code with the second for an appropriate Guzzle post request.
You can try this:
$donationData = [
'web_id' => $donation->web_id,
'amount' => $donation->amount,
'type' => $donation->type,
'date' => $donation->date->format('Y-m-d'),
'collection_id' => NULL,
'status_id' => $donation->status_id,
];
$multipart = [];
foreach ($uploadDocs as $doc) {
$multipart[] = [
[
'name' => 'donation_id',
'contents' => $donation->web_id,
],
[
'name' => 'type_id',
'contents' => $doc->type_id,
],
[
'name' => 'file',
'contents' => fopen($doc->path, 'r'),
'headers' => ['Content-Type' => 'application/pdf'],
],
];
}
Than perform your request:
$r = $client->request('POST', 'http://example.com', [
'body' => $donationData,
'multipart' => $multipart,
]);

Laravel validation rules for related checkboxes

In my form I have a couple checkboxes and i want at least one of them (or both) to be checked before submitting my form. My input checkboxes are named car and bike. In my controller i am validating my request
$this->validate($request, [
'content'=> 'required',
'title' => 'required',
'car | bike' => 'required', //this is not correct.
]);
What i couldn't figure is the statement that allows me to do the validation above
return Validator::make($request, [
'firstName' => 'required|max:255',
'lastName' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'checkbox' =>'required_without_all',
]);
OR
return Validator::make($request, [
'firstName' => 'required|max:255',
'lastName' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'option' =>'accepted'
]);
OR
Validator::make(
[ 'cats' => Input::get('cats') ],
[ 'cats' => 'min:1' ]
);
Examples:
$validator = Validator::make([
'cats' => ['Boots', 'Mittens', 'Snowball']
], ['cats' => 'min: 1']);
$result = $validator->fails(); // returns false
$validator = Validator::make([
'cats' => ['Boots', 'Mittens', 'Snowball']
], ['cats' => 'min: 2']);
$result = $validator->fails(); // returns false
$validator = Validator::make([
'cats' => ['Boots', 'Mittens', 'Snowball']
], ['cats' => 'min: 4']);
$result = $validator->fails(); // returns true
OR
Refer : https://laravel.com/docs/5.1/validation#available-validation-rules

required_if works incorrect

I use the following validation rule:
'email' => 'required_if:phone-register,false|email|max:255|unique:users',
And my incoming data are:
array:6 [▼
"_token" => "xPGUfppYirlLmSezTQduKZV4NXUAqf3I5DhrSJTZ"
"email" => null
"phone" => "514033390"
"phone-register" => "true"
"password" => "1234567"
"password_confirmation" => "1234567"
]
Despite on I have incoming email as null, the validation for email still works. Why, if I set required_if:phone-register,false
Full code of validation:
return Validator::make($data, [
'email' => 'required_if:phone-register,"false"|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'phone-register' => 'required|boolean',
'phone' => 'required_if:phone-register,"true"|numeric|min:6|max:15'
]);
Now I have:
protected function validator(array $data)
{
$v = Validator::make($data, [
'password' => 'required|min:6|confirmed',
'phone-register' => 'required',
'phone' => 'required_unless:phone-register,true|numeric|min:6'
]);
$v->sometimes('email', 'required|email|max:255|unique:users', function ($input) {
return $input->phone-register === "false";
});
}
You can add rules conditionally:
$v = Validator::(....);
$v->sometimes('email', 'required|email|max:255|unique:users', function ($input) {
return $input->phone-register === "false";
});

Resources