Laravel分配一对多(?),多对多(?)

时间:2017-09-13 10:10:15

标签: laravel eloquent relationship

我有两个型号 站 算

我目前正试图将几个操作员“保存”到一个工作站 但我也希望能够将同一个运营商“保存”到另一个站点。

示例:

+---------------------------------+
|   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);
}

在这里,我必须说我正在创建工作站并尝试添加操作员:

在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()"

我知道关系有问题,但我无法弄明白......提前谢谢你

3 个答案:

答案 0 :(得分:1)

回滚迁移 php artisan migrate: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 artisan migrate

您的电台型号:

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();
    }

答案 1 :(得分:0)

您需要一个中间StationsOperators表。

然后尝试更新您的运营商模型:

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

记住:

  

用于表示中间关系表的所有自定义模型都必须扩展Illuminate \ Database \ Eloquent \ Relations \ Pivot类。

这是一个很多关系。您必须使用数据透视表。 @ref:https://laravel.com/docs/5.5/eloquent-relationships#many-to-many

答案 2 :(得分:0)

关联方法仅适用于belongsTo关系,因此它不会为多对多关系工作。

您可以将attach方法用于多对多,这是laravel doc https://laravel.com/docs/5.5/eloquent-relationships#updating-many-to-many-relationships

中的示例