我有两个模型车站操作员
我目前正在尝试将几个运营商"保存"到电台但是我也希望能够将同一操作员"保存"到另一个站。
示例:
+---------------------------------+
| Station | Operator(s) |
|---------------------------------|
| Munich | Lufthansa |
| | KLM |
| | Air Malta |
|---------------------------------|
| Berlin | Lufthansa |
| | KLM |
|---------------------------------|
|------- etc ---------------|
|---------------------------------|
我的电台表:
Schema::create('stations', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 100);
$table->timestamps();
});
我的电台模型:
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和站点的名称后:
$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 IlluminateDatabaseQueryBuilder::associate()"
我知道关系有问题,但我无法弄清楚...谢谢您提前
回滚您的迁移 PHP Artisan迁移:Rollback
像这样更改操作员表迁移,
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工匠迁移
您的电台模型:
public function StationOperators() {
return $this->hasMany(StationOperators::class);
}
您的操作员模型:
public function StationOperators() {
return $this->hasMany(StationOperators::class);
}
您的站点模型:
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 AppStationOperators();
$stationOperator->save();
}
您需要一个中间StationsOperators
表。
然后尝试更新操作员模型:
public function stations() {
return $this->belongsToMany(Stations::class)->using(StationsOperators::class);
}
记住:
所有用于表示关系中间表的自定义模型都必须扩展照明数据库 eloquent resstance rothate pivot类。
这是一个多种关系。您必须使用枢轴表。@ref:https://laravel.com/docs/5.5/eloquent-relationships#many-to-many
仅适用于属于属于关系的关联方法,因此对许多关系都无法使用。
您可以对许多人使用附件方法,以下是Laravel Doc https://laravel.com/docs/5.5/eloquent-relationships#updating-many-to-many-to-many-relationships