Laravel 5.4를 사용하고 있습니다. 나는 2 개의 테이블 destination
과 user
과 피벗 테이블 destination_user
을 가지고있다.피봇 테이블 데이터를 사용하여 관계 테이블 정보 가져 오기
대상 테이블
---|------
id | name
---|------
1 | sth
사용자 테이블
---|------
id | name
---|------
1 | sth
내가 destinationUser
라는 이름의 피벗 테이블에 대한 모델을 만들어
--------------|--------
destination_id| user_id
--------------|--------
1 | 1
2 | 1
3 | 2
마지막으로 피벗 테이블.
내 destination
모델은 다음과 같습니다
<?php
namespace App\models;
use App\Models\User;
use App\Models\DestinationUser;
use App\Models\DestinationImage;
use Illuminate\Database\Eloquent\Model;
class Destination extends Model
{
protected $table = 'destinations';
public function user() {
return $this->belongsToMany('App\Models\User');
}
public function destinationUser() {
return $this->hasMany('App\Models\DestinationUser');
}
}
내가 각각의 사용자 세부 사용하여 피벗 테이블의 모든 대상을 싶어. 지금까지 노력이있다 한 :
$destinations = $this->destination->with('user', 'destinationUser')
->whereHas('destinationUser', function($query) {
$query->where('user_id', user()->id);})
->paginate(20);
dd($destinations[0]->destinationUser);
나에게 destination id
및 user id
을 제공하지만 사용자 세부 사항을 원한다. 이것을 어떻게 할 수 있습니까? 당신의 도움이
많은 관계가 필요합니다. https://laravel.com/docs/5.5/eloquent-relationships#many-to-many – madalinivascu
@madalinivascu – vijayrana
피봇 테이블의 테이블 이름은 무엇입니까? – madalinivascu