Laravel分配一对多(?),多对多(?)
问题描述:
我有两个型号 站 运营商Laravel分配一对多(?),多对多(?)
我目前正在为若干运营“保存”到车站 但我想也是要能够将相同的操作员“保存”到另一个站。
实施例:
+---------------------------------+
| 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);
}
在这里,我必须说,我创造了站,并试图加入运营商:
在StationsController:
收到了运营商的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 Illuminate\Database\Query\Builder::associate()"
我知道有一些错误的关系,但我不能看着办吧......谢谢你在前进
答
你需要一个中间StationsOperators
表。
然后尝试更新您的运营模式是这样的:
public function stations() {
return $this->belongsToMany(Stations::class)->using(StationsOperators::class);
}
记住:
用来表示关系的中间表必须扩展照亮\数据库\雄辩\关系\枢轴所有定制机型类。
这是一个多关系。你必须使用数据透视表。 @ref:https://laravel.com/docs/5.5/eloquent-relationships#many-to-many
答
回滚迁移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 artisan 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();
}
答
该关联方法仅适用于belongsTo关系,因此它不适用于多对多关系。
您可以使用连接方法多对多,这里是从laravel DOC https://laravel.com/docs/5.5/eloquent-relationships#updating-many-to-many-relationships
存在迁移的运营商表中没有外键引用的例子。那么关系将如何运作? – Naveen
@Naveen你可能会精心制作吗? – Jim