【发布时间】:2017-07-04 16:05:03
【问题描述】:
我有来自其他列的 3 个 ID 的表。如何附加2个以上的ID?一世 不知道如何连接三把钥匙。当我尝试做“ $weather->parks()->attach('1')->users()->attach('2');”它不附加park_id。 请帮忙。
Schema::create('weather_user_park', function (Blueprint $table) {
$table->integer('weather_id')->unsigned();
$table->integer('park_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('weather_id')->references('id')->on('weathers');
$table->foreign('park_id')->references('id')->on('parks');
$table->foreign('user_id')->references('id')->on('users');
});
我也有 3 个模型:
Park.php
public function weathers()
{
return $this->belongsToMany('App\Weather', 'weather_user_park');
}
public function users()
{
return $this->belongsToMany('App\User', 'weather_user_park');
}
User.php
public function weathers()
{
return $this->belongsToMany('App\Weather', 'weather_user_park');
}
public function parks()
{
return $this->belongsToMany('App\Park', 'weather_user_park');
}
Weather.php
public function users()
{
return $this->belongsToMany('App\User', 'weather_user_park');
}
public function parks()
{
return $this->belongsToMany('App\Park', 'weather_user_park');
}
【问题讨论】:
-
您是否尝试过不以这种方式链接方法?并单独调用它?。
标签: laravel relationship foreign-key-relationship