How can I update an existing Eloquent relationship in Laravel 4?

故事扮演 提交于 2019-12-04 17:41:53

问题


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?

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:

$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.


回答1:


Taylor Otwell's official answer is the following:

$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!




回答2:


Hey you can refer the laravel docs for the solution.

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

$account = Account::find(10);

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

$user->save();



回答3:


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

  //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

  //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




回答4:


**

Try

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

My apologies, I didn't understand what was trying to be accomplished.



来源:https://stackoverflow.com/questions/16921511/how-can-i-update-an-existing-eloquent-relationship-in-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!