2017-09-13 6 views
0

に多くの割り当て 駅 演算子Laravelの多くの一つ(?)、多くの(?)私は2つのモデルを持っている

私は現在、駅 にいくつかの演算子を「保存」しようとしていますが、私はあることをも望みます同じオペレータを別のステーションに「保存」することができます。

例:

+---------------------------------+ 
| Station | Operator(s) | 
|---------------------------------| 
| Munich  | Lufthansa  | 
|    | KLM    | 
|    | Air Malta  | 
|---------------------------------| 
| Berlin  | Lufthansa  | 
|    | KLM    | 
|---------------------------------| 
|------- etc ---------------| 
|---------------------------------| 

マイステーション表:

​​

マイステーションモデル:

public function operators() { 
    return $this->hasMany(Operators::class); 
} 

マイ演算子表:

Schema::create('operators', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name', 100)->unique(); 
     $table->string('email', 100); 
     $table->boolean('notify')->default(false); 
     $table->timestamps(); 
    }); 

マイ演算子モデル:

ここ
public function stations() { 
    return $this->belongsTo(Stations::class); 
} 

私は駅を作成し、演算子を追加しようとしていると言わなければならない:演算子のIDを受け取った後

と:StationsControllerで

駅の名称:

$station = new Stations; 
    $station->name = request('name'); 
    $station->save(); 
    foreach (request('operators') as $operator) { 
     $tempOperator = Operators::find($operator); 
     $station->operators()->associate($tempOperator)->save(); 
    } 

応答は次のとおりです。

"Call to undefined method Illuminate\Database\Query\Builder::associate()" 

私が関係に問題がある知っているが、私はそれを把握することはできません...あなたが中間StationsOperatorsテーブルを必要とし、事前

+0

の例です。関係がどのように機能するのでしょうか? – Naveen

+0

@Naveenあなたは多分詳細を教えてくれますか? – Jim

答えて

0

でいただきありがとうございます。

次に、このようなあなたのオペレータモデルを更新してみてください:

public function stations() { 
    return $this->belongsToMany(Stations::class)->using(StationsOperators::class); 
} 

を忘れないでください:

を照らし\データベース\雄弁\リレーションズ\ピボットを拡張する必要があり関係の中間テーブルを表すために使用されるすべてのカスタムモデル

クラス。

これは多対多の関係です。ピボットテーブルを使用する必要があります。 @ref:https://laravel.com/docs/5.5/eloquent-relationships#many-to-many

+0

同じ結果です。しかし、試していただきありがとうございます – Jim

+0

私は何かが恋しい、今すぐお試しください – aaron0207

+0

ピボットステーションの作成者を作成しました。それと同じ結果。それが空であるか、何かを追加する必要がありますか? – Jim

1

移行PHPの職人の移行をロールバック:ロールバック

の変化このようなあなたの演算子テーブルの移行、あなたのようなマッピングテーブルを作成する必要が

Schema::create('operators', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name', 100)->unique(); 
     $table->string('email', 100); 
     $table->boolean('notify')->default(false); 
     $table->timestamps(); 
    }); 

Schema::create('station_operators', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('stations_id')->unsigned(); 
     $table->integer('operators_id')->unsigned(); 
     $table->timestamps(); 

     $table->foreign('stations_id')->references('id')->on('stations'); 
     $table->foreign('operators_id')->references('id')->on('operators'); 
    }); 

マイグレーション実行php職人miすりおろ

あなたのステーションモデル:

public function StationOperators() { 
    return $this->hasMany(StationOperators::class); 
} 

あなたの演算子モデル:

public function StationOperators() { 
    return $this->hasMany(StationOperators::class); 
} 

あなたStationOperatorsモデル:仲間のために

public function Stations() { 
    return $this->belongsTo(Stations::class); 
} 

public function Operators() { 
    return $this->belongsTo(Operators::class); 
} 

$station = new Stations; 
    $station->name = request('name'); 
    $station->save(); 
    foreach (request('operators') as $operator) { 
     // $tempOperator = Operators::find($operator); 
     // $station->StationOperators()->associate($tempOperator)->save(); 

     $data = [ 
      'stations_id' => $station->id, 
      'operators_id' => $operator, 
     ]; 

     $stationOperator = new \App\StationOperators(); 
     $stationOperator->save(); 
    } 
+0

残念ながら同じ結果でした... – Jim

+0

私の答えが更新されました。それが役に立てば幸い。 – Naveen

+0

ありがとうございました!まだ同じことが起こっています。トリプルすべてをチェックし、私はそれがすべて同じ.. StationOperatorsモデルはモデルかピボットであるべきですか? – Jim

関連する問題