2017-03-29 4 views
1

Ich möchte alle Projekte durch eine Gruppe erhalten. Ich habe das jetzt funktioniert, aber ich kann nicht die richtigen IDs bekommen. Ich bekomme 1 als ID für alle meine Posts. Mache ich etwas falsch?Laravel Model - Alle IDs 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' 
    ); 
} 

Dump (alle Einzelteile haben das gleiche Attribut> id, alle anderen Attribute sind gut)

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 ▶} 

Antwort

0

Sie verwenden die gleichen IDs, um sie zu erhalten. Versuchen Sie, den Code zu ändern:

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

Dies funktioniert nicht. Jeder Benutzer ist in einer Gruppe. Jede Gruppe hat mehrere Projekte und mehrere Benutzer. –