[英]Laravel 4 - Insert through hasMany relationship
这是我的模特关系...
class User extends Eloquent {
public function loginLog(){
return $this->hasMany('LoginLog');
}
}
class LoginLog extends Eloquent {
public function user(){
return $this->belongsTo('User');
}
}
当我将数据插入数据库中的login_logs
表中时,所有数据均已正确输入,但未将用户ID插入到user_id中 (laravel希望这样做)。
这是我要插入login_logs
。
$user->loginLog()->insert(array(
'user_id' => $user->id, //I could put it here, but then what is the point in a relationship?
'email' => $user->email,
'ip_address' => Request::getClientIp(),
'country_code' => $country_code,
'status' => $status,
'created_at' => Helper::dateTimeNow()
));
您必须附加用户。
它位于文档http://laravel.com/docs/eloquent#inserting-related-models中
更新:
在重读您的问题时,我认为您想在执行$user->loginLog()->insert
而不是$loginLog->insert
首先通过其ID查找用户。
尝试链接它: $user::find($theIDYouWant)->loginLog()->insert
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.