2016-11-29 12 views
-1

belongsToManyの関係belongsToManyからデータを取得する必要があります。私のモデルのA-> B-> Cはproviders-> caption-> eventTypeなので、すべてのプロバイダをイベントタイプから取り出します。BelongsToMany BelongsToMany関係を持つ

モデルは次のようになります。

プロバイダモデル

Class Provider extends Model { 
    public function captions() { 
     return $this->belongsToMany(Caption::class); 
    } 
} 

キャプションモデル

Class Caption extend Model { 
    public function event_type() { 
     return $this->belongsToMany(Event_type::class); 
    } 
    public function providers() { 
     return $this->belongsToMany(Provider::class); 
    } 
} 

イベントの種類モデル

Class Event_type extends Model { 
    public function captions() { 
     return $this->belongsToMany(Caption::class); 
    } 
} 

データベースは、次のようになります。

プロバイダ

ID

たevent_type

ID

のキャプション

caption_event_type

caption_id

がevent_type_id ID

caption_provider

caption_id

PROVIDER_ID

Thnks。

+0

あなたの関係やモデルを追加してください。そうしないと、あなたを助ける方法がありません。 – martinczerwi

+0

モデルは、パートナーSaumya Rastogiにコメントされているものと同じです。 –

答えて

0

は私が私たちのモデル構造を理解することによると、このように書きください。

class Provider extends Model { 
    public function captions() 
    { 
     return $this->belongsToMany(Caption::class); 
    } 
} 

class EventType extends Model { 
    public function captions() 
    { 
     return $this->belongsToMany(Caption::class); 
    } 
} 

class Caption extends Model { 
    public function providers() 
    { 
     return $this->belongsToMany(Provider::class); 
    } 

    public function eventTypes() 
    { 
     return $this->belongsToMany(EventType::class); 
    } 
} 

EventTypeのためのすべてのprovidersを得るために、あなたはこのようにそれを得る:

$captions = EventType::find(1)->captions; 

foreach($captions as $caption) 
{ 
    $providers_arr[] = $caption->providers; 
} 

$providers_collection = collect($providers_arr)->unique(); 

・ホープ、このことができます!

+0

こんにちは、私はモデル内の関係をどのようにしているのですか、データを$ event-> caption ) - >プロバイダ私はこのエラーが表示されます:未定義のプロパティ:Illuminate \ Database \ Eloquent \ Collection :: $ providers –

+0

ああ、それは私の悪いです、上記の私の更新された答えを参照してください! –

+0

おかげで兄弟、それは私の多くを助けた –

関連する問題