Receiving [Illuminate\Database\Eloquent\MassAssignmentException] email using Laravel 5.5 - laravel

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.

Related

Setting default value for Laravel Migration

I am using Laravel 5.6 and has a multi-auth funcitonality and it works fine. I have user and admin tables in my DB. How can i set a default value on admin table like NAME, EMAIL and PASSWORD? So everytime i run php artisan migrate:refresh i will have the default NAME EMAIL and PASSWORD in admin table? Thank you!
Without a little clarification there are two possible answers to your question. If you are asking about columns in your database tabled having default values this has already been answered but it is simply by chaining the method default on your attribute.
$table->tinyInteger('status')->default('1');
If you are asking about populating a row in your database whenever you migrate refresh. You can use seeders:
Laravel includes a simple method of seeding your database with test
data using seed classes. All seed classes are stored in the
database/seeds directory. Seed classes may have any name you wish, but
probably should follow some sensible convention, such as
UsersTableSeeder, etc. By default, a DatabaseSeeder class is defined
for you. From this class, you may use the call method to run other
seed classes, allowing you to control the seeding order.
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('users')->insert([
'name' => 'default_name',
'email' => 'default#mail.com',
'password' => 'password',
]);
}
}
And afterwards just call:
php artisan migrate:refresh --seed

is there an available laravel validation rules for old password?

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);
}),
],
]);

Laravel 5 creating custom registration and login from default Authentication

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

best way for bulk insertion validation laravel 5

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.

Magento eav entity setup failing - Can't create table

I am trying to set up a custom entity in Magento 1.7.0.0, following alan storms article about it, but with this simple install script, it tells me that "Can't create table: eavblog_posts".
My install script is deadsimple and looks like this:
<?php
$installer = $this;
$installer->addEntityType('complexworld_eavblogpost',
Array(
'entity_model'=>'complexworld/eavblogpost',
'attribute_model'=>'',
'table'=>'complexworld/eavblogpost',
'increment_model'=>'',eav/entity_increment_numeric
'increment_per_store'=>'0'
));
$installer->createEntityTables(
$this->getTable('complexworld/eavblogpost')
);
How can i get my install script working? Is this a known magento bug?
In my case, the issue was due to a bug with the createEntityTables() method in Magento 1.7/1.12
In their answer to this bug report, the Magento team recommend commenting out line 417 from lib/Varien/Db/Adapter/Pdo/Mysql.php:
$this->_checkDdlTransaction($sql);
Instead, I would recommend following the advice in Zachary Schuessler's post and either
1) copying the createEntityTables() method to your own file (Your/Module/Model/Resource/Setup.php) and commenting out the transaction methods...
or
2) writing an abstracted query in order to keep transactions:
// Example of MySQL API
/**
* Create table array('catalog/product', 'decimal')
*/
$table = $installer->getConnection()
->newTable($installer->getTable(array('catalog/product', 'decimal')))
->addColumn('value_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
'identity' => true,
'nullable' => false,
'primary' => true,
), 'Value ID')
->addColumn(...
First of all, this line is wrong:
'increment_model'=>'',eav/entity_increment_numeric
It needs to be inside the quotes.
Failing that there are some bugs in the installer functions of the latest version.
Go into your database using phpMyAdmin or similar and check to see if any of the tables already exist. If they do, delete them. Also delete the module's record in core_resource.
Try again.
Then there's a step in here I cant remember off the top of my head (useful, I know, but I'll try and remember it tonight and edit this).
Once the tables have been created, if you look at the foreign key assignments for the type tables (int, text char etc) you'll notice that the entity_id field is looking at eav_entity.entity_id. This needs to change to your eavblogpost_entity table.
You might also notice that the eavblogpost_entity.entity_id field is INT(11), when all the foreign key references are INT(10). Change the eavblogpost_entity.entity_id field to INT(10) manually.
The only way around all of this is to override the createEntityTables() function with one that works, or create all the tables manually. Here's a good resource to help you through that parthttp://inchoo.net/ecommerce/magento/creating-an-eav-based-models-in-magento/
Mess around with all of these as you go and I'm sure you'll stumble across the step that you have to do that I've forgotten. Sorry!

Resources