14

Laravel で 1 対多の関係を更新しようとしています。残念ながら、それに関するドキュメントは見つかりませんでした。誰でも私を助けることができますか?

これは私がこれまでに持っているものです:

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() メソッドの両方を使用して多対多の関係で機能し、テーブル間の関係を (関係の両側から) 更新します。1 対多の関係では、attach() メソッドはサポートされていないようです。

4

4 に答える 4

37

テイラー・オトウェルの公式回答は次のとおりです。

$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 に答える