这里真的很奇怪。我正在通过\Console\Command
为我的网站生成管理员,在某些时候我显然是在创建它。
$user = User::create([
'first_name' => 'Admin',
'last_name' => 'Inistrator',
'phone' => '',
'email' => $email,
'password' => bcrypt($password),
'role' => 'admin',
]);
我的数据库中的结果是该帐户获得user
个角色而不是admin
,但如果我这样做了
$user = User::create([
'first_name' => 'Admin',
'last_name' => 'Inistrator',
'phone' => '',
'email' => $email,
'password' => bcrypt($password),
'role' => 'admin',
]);
$user->role = 'admin';
$user->save();
然后它完美无缺。我怀疑Laravel 5在我创建一个新帐户时会做一些奇怪的事情,这个帐户会与所有特征相关联,等等与User
模型相关联......你觉得怎么样?
PS:这是我的迁移表
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');
}
}
答案 0 :(得分:4)
我发现了问题。我听了DB::getQuerylog()
的问题(我也可以在答案中使用DB::listen
)
查询是:
\DB::enableQueryLog();
$user = User::create([
'first_name' => 'Admin',
'last_name' => 'Inistrator',
'phone' => '',
'email' => $email,
'password' => bcrypt($password),
'role' => 'admin',
]);
dd(\DB::getQueryLog());
结果是:
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
]
]
所以我意识到我的田地被忽略了。我检查了模型,看到我忘了在$fillable
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];
我添加了其他字段,就像魅力一样。所以,如果有人遇到类似的问题,解决方案非常简单,不要忘记填充$fillable
数组;)
答案 1 :(得分:1)
尝试此操作来监听查询:
DB::listen(function($sql, $bindings, $time)
{
//
});
答案 2 :(得分:1)
可能会发生两件事,
$fillable
值不匹配protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
GridBind();
}
}
public void GridBind()
{
int Year = DateTime.Now.Year;
int Month = DateTime.Now.Month;
int curMonth = DateTime.DaysInMonth(Year, Month);
DataTable Dt = new DataTable();
SqlDataAdapter Adp = new SqlDataAdapter("select Distinct e.EmpId,e.FirstName,e.Designation, count(a.status) as 'Present Days' ,('" + curMonth + "'-count(a.status)) as 'Absent Days','" + curMonth + "' as'Total Days' from EmployeeMaster e , attendance a where a.status='Absent' and e.EmployeeId=a.EmployeeId group by e.EmpId,e.firstName,e.Designation", sqlcon);
Adp.Fill(Dt);
gvattendance.DataSource = Dt;
gvattendance.DataBind();
}
数组