2017-03-29 3 views
1

すべてのプロジェクトをグループにまとめたい。私は今すぐ働いているが、私は正しいIDを得ることができない。私はすべての私のポストのIDとして1を得続けます。私は何か間違っているのですか?Laravel Model - すべてのIDの返信1

ProjectController.php

public function index() 
{ 
    $projects = \Auth::user()->projects; 
    dd($projects); 
    return view('projects.home', compact('projects')); 
} 

User.php

public function projects() 
{ 
    return $this->hasManyThrough(
     'App\Project', 'App\Group', 
     'id', 'group_id', 'group_id' 
    ); 
} 

(すべての項目が同じ属性> IDを持って、他のすべての属性が優れている)

ダンプ
Collection {#224 ▼ 
    #items: array:8 [▼ 
    0 => Project {#225 ▼ 
     #table: "projects" 
     #connection: null 
     #primaryKey: "id" 
     #keyType: "int" 
     +incrementing: true 
     #with: [] 
     #perPage: 15 
     +exists: true 
     +wasRecentlyCreated: false 
     #attributes: array:7 [▼ 
     "id" => 1 
     "name" => "project.com" 
     "display_name" => "Project 1" 
     "group_id" => 1 
     "active" => 0 
     "created_at" => null 
     "updated_at" => "2017-03-29 12:21:47" 
     ] 
     #original: array:7 [▶] 
     #casts: [] 
     #dates: [] 
     #dateFormat: null 
     #appends: [] 
     #events: [] 
     #observables: [] 
     #relations: [] 
     #touches: [] 
     +timestamps: true 
     #hidden: [] 
     #visible: [] 
     #fillable: [] 
     #guarded: array:1 [▶] 
    } 
    1 => Project {#226 ▶} 

答えて

0

同じIDを使用して取得しています。コードを次のように変更してください。

public function projects() 
{ 
    return $this->hasManyThrough(
     'App\Project', 'App\Group', 
     'id', 'user_id', 'group_id' 
    ); 
} 
+0

これは機能しません。すべてのユーザーは1つのグループに属します。すべてのグループには、複数のプロジェクトと複数のユーザーが存在します。 –

関連する問題