2017-03-29 47 views
1

我想通過一個組獲得所有項目。我現在正在工作,但我似乎無法獲得正確的ID。我不斷收到1作爲我的所有帖子的ID。難道我做錯了什麼?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

這不起作用。每個用戶都在一個組中。每個小組都有多個項目和多個用戶。 –