2017-02-15 4 views
0

У меня есть таблица с 3 ID из других столбцов. Как подключить более 2 ID? I Не знаю, как приложить три ключа. Когда я пытаюсь сделать «$ weather-> parks() -> attach ('1') -> users() -> attach ('2'); он не прикрепляет park_id. Пожалуйста, помогите.Laravel 5 multiple attach

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

вы пробовали не Цепочка методы, что путь? и просто назовите его отдельно ?. – Gntem

ответ

0

Park.php

public function users() 
{ 
    return $this->belongsToMany('App\User', 'park_user')->withPivot('weather'); 
} 

User.php

public function parks() 
{ 
    return $this->belongsToMany('App\Park', 'park_user')->withPivot('weather'); 
} 

park->users(); дает информацию о парке

Вы можете добавить информацию к пользователю, как так:

$park = App\Park::find(1); 
$weather = App\Weather::find($id); 
$user->parks()->attach($park->id, ['weather' => $weather->id]); 

удалить отношения

$user->parks()->detach($park->id);