如何更新 Laravel 4 中现有的 Eloquent 关系?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16921511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I update an existing Eloquent relationship in Laravel 4?
提问by santacruz
I am trying to update the relationship of a one-to-many relationship in Laravel. Unfortunately I couldn't find any documentation for it. Can anyone help me?
我正在尝试更新 Laravel 中一对多关系的关系。不幸的是,我找不到任何相关文档。谁能帮我?
This is what I have so far:
这是我到目前为止:
class Account extends Eloquent
{
public function users()
{
return $this->hasMany('User');
}
}
class User extends Eloquent
{
public function account()
{
return $this->belongsTo('Account');
}
}
Now I am trying to update the relationship from USER(1) > ACCOUNT(50) to USER(1) > ACCOUNT(99). How would I do this? I tried the following:
现在我正在尝试将关系从 USER(1) > ACCOUNT(50) 更新为 USER(1) > ACCOUNT(99)。我该怎么做?我尝试了以下方法:
$account = Account::find(99);
User::find(1)->account()->save($account);
But that doesn't work :-( Any help deeply appreciated!!
但这不起作用:-(任何帮助深表感谢!!
UPDATE:
更新:
The following works:
以下工作:
$user = User::find(1);
$user->account_id = 99;
$user->save();
...but there MUST be a better solution like the one above, right?
...但必须有一个更好的解决方案,比如上面的那个,对吧?
It works in many-to-many relationships with both the save() and attach() method to update the relationship between tables (from both sides of the relationship). In one-to-many relationships the attach() method doesn't seem to be supported.
它与 save() 和 attach() 方法在多对多关系中工作,以更新表之间的关系(从关系的双方)。在一对多关系中,似乎不支持 attach() 方法。
回答by santacruz
Taylor Otwell's official answer is the following:
Taylor Otwell 的官方回答如下:
$account = Account::find(99);
User::find(1)->account()->associate($account)->save();
I couldn't find the associate() method in the official docs. So if someone else is looking for a solution to this. Here you go!
我在官方文档中找不到 associate() 方法。因此,如果其他人正在寻找解决方案。干得好!
回答by Vivek Pandey
Hey you can refer the laravel docs for the solution.
嘿,您可以参考 laravel 文档以获取解决方案。
Link http://laravel.com/docs/4.2/eloquent#inserting-related-models
链接http://laravel.com/docs/4.2/eloquent#inserting-related-models
$account = Account::find(10);
$user->account()->associate($account);
$user->save();
回答by evandertino
This is how i would have done it
这就是我会这样做的方式
//step 1
My migrations
我的迁移
class CreateUsersTable extends Migration {
public function up()
{
Schema::create('users', function(Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->timestamps();
});
}
public function down()
{
Schema::drop('users');
}
}
class CreateUserDetailsTable extends Migration {
public function up()
{
Schema::create('user_details', function(Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('user_id')->unsigned()->unique('user_id', 'user_details_user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->enum('gender', array('male', 'female'))->nullable();
$table->string('city')->nullable();
$table->date('date_of_birth')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::drop('user_details');
}
}
Then
然后
//step 2
My base models
我的基本模型
class UserDetail extends Eloquent {
protected $guarded = array();
protected $table = 'user_details';
public function user()
{
return $this->belongsTo('User');
}
}
class User extends Eloquent {
protected $table = 'users';
protected $guarded = array();
public function detail()
{
return $this->hasOne('UserDetail');
}
}
Then
然后
//step 3
My Controllers - Updating user_details schema
我的控制器 - 更新 user_details 架构
//When Updating records i get the raw data from say an input
$detail = Input::all();
//then find the user
$user = User::find(1);
//then update the details
$detail = $user->detail()->update($detail);
//then respond back with the detail
Response::json($detail);
My Controllers - creating user_details schema
我的控制器 - 创建 user_details 架构
//again get data input from source, the source does not come with user id
$detail = Input::all();
//when creating new record
$detail = new UserDetail($detail);
$detail = $user->detail()->save($detail);
return Response::json($detail);
And thats it for creating and updating new records using Laravel 4's belongsTo relationships
这就是使用 Laravel 4 的belongsTo 关系创建和更新新记录的方法
回答by SamSquanch
**
**
Try
尝试
$user = User::find();
$account = Account::find()->user()->save($user)
My apologies, I didn't understand what was trying to be accomplished.
我很抱歉,我不明白要完成什么。