如何更新Laravel 4中现有的Eloquent关系?

san*_*ruz 14 laravel laravel-4

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

这是我到目前为止:

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

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

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

$account = Account::find(99);

User::find(1)->account()->save($account);
Run Code Online (Sandbox Code Playgroud)

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

更新:

以下作品:

$user = User::find(1);
$user->account_id = 99;
$user->save();
Run Code Online (Sandbox Code Playgroud)

......但是必须有一个比上面更好的解决方案,对吗?

它与save()和attach()方法在多对多关系中工作,以更新表之间的关系(来自关系的两侧).在一对多关系中,似乎不支持attach()方法.

san*_*ruz 34

Taylor Otwell的官方答复如下:

$account = Account::find(99);
User::find(1)->account()->associate($account)->save();
Run Code Online (Sandbox Code Playgroud)

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

  • 答案现在也在官方文档中:http://laravel.com/docs/eloquent#inserting-related-models (6认同)

eva*_*ino 7

这就是我会这样做的方式

 //step 1
Run Code Online (Sandbox Code Playgroud)

我的迁移

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

 }
Run Code Online (Sandbox Code Playgroud)

然后

 //step 2
Run Code Online (Sandbox Code Playgroud)

我的基本模型

 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');
     }
 }
Run Code Online (Sandbox Code Playgroud)

然后

 //step 3
Run Code Online (Sandbox Code Playgroud)

我的控制器 - 更新 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);
Run Code Online (Sandbox Code Playgroud)

我的控制器 - 创建 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);
Run Code Online (Sandbox Code Playgroud)

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


小智 6

嘿,你可以参考laravel docs来解决问题.

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

$account = Account::find(10);

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

$user->save();
Run Code Online (Sandbox Code Playgroud)