14

我正在尝试更新 Laravel 中一对多关系的关系。不幸的是,我找不到任何文档。谁能帮我?

这是我到目前为止所拥有的:

class Account extends Eloquent 
{
     public function users()
     {
         return $this->hasMany('User');
     }
}

class User extends Eloquent 
{
     public function account()
     {
         return $this->belongsTo('Account');
     }
}

现在我正在尝试将关系从 USER(1) > ACCOUNT(50) 更新为 USER(1) > ACCOUNT(99)。我该怎么做?我尝试了以下方法:

$account = Account::find(99);

User::find(1)->account()->save($account);

但这不起作用:-(任何帮助深表感谢!

更新:

以下作品:

$user = User::find(1);
$user->account_id = 99;
$user->save();

...但是必须有一个像上面这样更好的解决方案,对吧?

它使用 save() 和 attach() 方法在多对多关系中工作,以更新表之间的关系(从关系的双方)。在一对多关系中,似乎不支持 attach() 方法。

4

4 回答 4

37

Taylor Otwell 的官方回答如下:

$account = Account::find(99);
User::find(1)->account()->associate($account)->save();

我在官方文档中找不到 associate() 方法。因此,如果其他人正在寻找解决方案。干得好!

于 2013-06-05T15:45:56.953 回答
7

这就是我会做的

 //step 1

我的迁移

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');
     }

 }

然后

 //step 2

我的基础模型

 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');
     }
 }

然后

 //step 3

我的控制器 - 更新 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);

我的控制器 - 创建 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);

这就是使用 Laravel 4 的 belongsTo 关系创建和更新新记录

于 2013-07-04T09:47:25.703 回答
6

嘿,您可以参考 laravel 文档以获取解决方案。

链接http://laravel.com/docs/4.2/eloquent#inserting-related-models

$account = Account::find(10);

$user->account()->associate($account);

$user->save();
于 2014-09-28T15:47:52.927 回答
0

**

尝试

$user = User::find();
$account = Account::find()->user()->save($user)

抱歉,我不明白要完成什么。

于 2013-06-05T02:36:50.750 回答