Call to undefined method IlluminateDatabaseQueryBuilder::associate()(调用未定义的方法 IlluminateDatabaseQueryBuilder::associate())
问题描述
参考:如何更新Laravel 4 中现有的 Eloquent 关系?
$userinfo = Userinfo::find($id);
User::find($id)->userinfo()->associate($userinfo)->save();
我收到错误:Call to undefined method IlluminateDatabaseQueryBuilder::associate()
这里是整个方法:
public function saveUser($id)
{
$user = User::find($id);
$userdata = Input::all();
$rules = array(
'email' => 'required|email',
'state' => 'size:2',
'zip' => 'size:5',
'phone' => array('regex:/^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/')
);
$validator = Validator::make($userdata, $rules);
if ($validator->passes())
{
if ($userdata['email'] !== $user->email)
{
$rules = array('email' => 'unique:users');
$validator = Validator::make($userdata, $rules);
if ($validator->fails()) return Redirect::route('admin.user.edit', array('user' => $user))
->with('error', 'Specified email already exists.');
}
$user->email = $userdata['email'];
$user->firstname = $userdata['firstname'];
$user->lastname = $userdata['lastname'];
$userinfoArray = array(
'address' => $userdata['address'],
'city' => $userdata['city'],
'state' => $userdata['state'],
'zip' => $userdata['zip'],
'phone' => preg_replace('/[^0-9]/', '', $userdata['phone'])
);
$user->save();
if (!$user->userinfo)
{
$userinfo = new Userinfo($userinfoArray);
$userinfo = $user->userinfo()->save($userinfo);
}
else
{
$userinfo = Userinfo::find($id);
User::find($id)->userinfo()->associate($userinfo)->save();
//$user->userinfo()->update($userinfoArray);
}
return Redirect::route('admin.user.detail', array('id' => $id))
->with('success', 'User updated.');
}
return Redirect::route('admin.user.edit', array('id' => $id))
->withInput()
->withErrors($validator);
}
推荐答案
associate() 是belongsTo 关系的一个方法,但从上面看来,您试图通过hasOne 关系调用它.
associate() is a method of the belongsTo relationship, but it looks like from the above you are trying to call it via the hasOne relationship.
我只是猜测,因为您没有提供您雄辩的模型类代码,所以无法看到您是如何准确设置关系的,但如果您有:
I am just guessing as you have not provided your eloquent model class code so can't see how you have set the relationships exactly, but if you have:
class User extends Eloquent {
public function userinfo()
{
return $this->hasOne('Userinfo');
}
}
class Userinfo extends Eloquent {
public function user() {
return $this->belongsTo('User');
}
}
然后需要针对 Userinfo 调用关联,因为它具有关联 () 方法附加到的belongsTo 关系.
Then associate needs to be called against Userinfo as this has the belongsTo relationship to which the associate() method is attached.
例如
$user = User::find(4);
$userinfo = UserInfo::find(1);
$userinfo->user()->associate($user);
$userinfo->save();
将user_info表中的外键user_id设置为$user对象的id.
Will set the foreign key user_id in the user_info table to the id of the $user object.
看看你上面的代码,这似乎不是你真正想要做的,
Looking at your above code it doesn't appear that this is what you are actually trying to do and that the
$user->userinfo()->update($userinfoArray);
您注释掉的调用实际上会执行您似乎想要实现的目标,即更新与当前用户相关的用户信息(如果该用户已存在).
call which you have commented out will in fact do what you seem to be trying to achieve, which is to update the userinfo that is related to the current user if that user already exists.
希望这会有所帮助.
格伦
这篇关于调用未定义的方法 IlluminateDatabaseQueryBuilder::associate()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:调用未定义的方法 IlluminateDatabaseQueryBuilder::associate()
基础教程推荐
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01