Laravel模型 - 所有ID返回1

时间:2017-03-29 13:38:09

标签: php laravel laravel-5

我希望通过一个小组获得所有项目。我现在已经开始工作,但我似乎无法获得正确的ID。我持续获得1作为我所有帖子的ID。我做错了吗?

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

1 个答案:

答案 0 :(得分:0)

您使用相同的ID来获取它们。尝试将代码更改为:

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