Reputation: 2178
Really weird issue here. I'm generating an administrator for my website through the \Console\Command
and at some point I obviously create it.
$user = User::create([
'first_name' => 'Admin',
'last_name' => 'Inistrator',
'phone' => '',
'email' => $email,
'password' => bcrypt($password),
'role' => 'admin',
]);
The result in my database is that the account got a user
role instead of admin
, but if I do this
$user = User::create([
'first_name' => 'Admin',
'last_name' => 'Inistrator',
'phone' => '',
'email' => $email,
'password' => bcrypt($password),
'role' => 'admin',
]);
$user->role = 'admin';
$user->save();
Then it works perfectly. I suspect Laravel 5 to do something weird while I create a new account, which would be linked with all the traits, etc. linked with the User
model ... What do you think ?
PS : Here's my migration table
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('phone');
$table->string('email')->unique();
$table->string('password', 60);
$table->enum('role', ['user', 'business', 'admin']);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
Upvotes: 2
Views: 7886
Reputation: 649
Two things can happen,
enum
value is not matching $fillable
arrayUpvotes: 2
Reputation: 2178
I found the problem. I listened the queries thank to the DB::getQuerylog()
(I also could have used DB::listen
as said in the answers)
The query is :
\DB::enableQueryLog();
$user = User::create([
'first_name' => 'Admin',
'last_name' => 'Inistrator',
'phone' => '',
'email' => $email,
'password' => bcrypt($password),
'role' => 'admin',
]);
dd(\DB::getQueryLog());
And the result is :
array:1 [
0 => array:3 [
"query" => "insert into `users` (`email`, `password`, `updated_at`, `created_at`) values (?, ?, ?, ?)"
"bindings" => array:4 [
0 => "klklm"
1 => "$2y$10$RUorxAp4EbN/7TvEmdk.dudBBb0dgUWhmAvRsjXgVFubhEKbaLR4K"
2 => "2015-04-13 10:30:36"
3 => "2015-04-13 10:30:36"
]
"time" => 3.17
]
]
So I realized my fields were simply ignored. I checked the model and saw I forgot to add the fields within $fillable
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];
I added the other fields and it works like a charm. So if anyone got a similar problem the solution is pretty simple, don't forget to fill the $fillable
array ;)
Upvotes: 5
Reputation: 1360
Try this to listen queries:
DB::listen(function($sql, $bindings, $time)
{
//
});
Upvotes: 1