我正在从L3升级。一个客户有很多用户,而用户属于客户。
迁移:
Schema::create('customers', function($table)
{
$table->increments('id');
$table->string('name')->unique();
$table->index('name');
$table->string('full_name');
$table->string('driver');
$table->string('host');
$table->string('database');
$table->string('username');
$table->string('password');
$table->timestamps();
});
Schema::create('users', function($table)
{
$table->increments('id');
$table->string('email')->unique();
$table->index('email');
$table->integer('customer_id')->unsigned();
$table->foreign('customer_id')->references('id')->on('customers')->on_update('cascade')->on_delete('cascade');
$table->integer('domain_id')->unsigned();
$table->foreign('domain_id')->references('id')->on('domains')->on_update('cascade')->on_delete('cascade');
$table->string('password');
$table->string('name');
$table->string('state');
$table->string('verification_token');
$table->string('resend_verification_token');
$table->string('change_password_token');
$table->string('last_ip');
$table->timestamp('last_login');
$table->timestamp('verification_timestamp');
$table->timestamp('change_password_timestamp');
$table->timestamps();
});
楷模:
class Customer extends Eloquent {
public function users()
{
return $this->hasMany('User');
}
}
class User extends Eloquent {
public function customer()
{
return $this->belongsTo('Customer');
}
}
但是按以下方式尝试这种关系:
$user = User::find(1);
echo $user->customer->name;
抛出一个异常(“试图获取非对象的属性”),因为customer为null。
并尝试:
$user = User::find(1)->customer();
引发异常(调用未定义的方法Illuminate \ Database \ Query \ Builder :: customer())。
我究竟做错了什么?
我已经将Laravel附带的用户模型重命名为User_.php,并用我自己的模型替换了它。
似乎没有以某种方式调用替换模型。一旦我完全删除了原始用户模型,一切都将正常进行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句