Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 459
santacruz Avatar asked Jun 04 '13 15:06

santacruz


2 Answers

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!

like image 191
santacruz Avatar answered Oct 02 '22 18:10

santacruz


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

like image 33
evandertino Avatar answered Oct 02 '22 20:10

evandertino