【问题标题】:laravel why i cant use releationshiop for data get with joinlaravel 为什么我不能使用 releationshiop 来获取数据
【发布时间】:2023-03-15 11:45:02
【问题描述】:

这是我的控制器:

 $homeTeamFacts = DB::table('match_facts')
            ->where('match_id', $match->id)
            ->join('players', 'match_facts.player_id', '=', 'players.id')
            ->where('players.team_id', '=', $homeTeamId)
            ->get();

这是我得到的数据收集:

Illuminate\Support\Collection {#1311 ▼
  #items: array:4 [▼
    0 => {#1325 ▼
      +"id": 2
      +"match_id": "1"
      +"player_id": "2"
      +"minutes": 90
      +"goals": null
      +"yellowCard": 1
      +"redCard": 0
      +"bench": 0
      +"created_at": "2021-01-08 11:12:59"
      +"updated_at": "2021-01-08 11:12:59"
      +"team_id": "1"
      +"position": "Gynėjas"
      +"photo": null
      +"name": "Tomas"
      +"surname": "Tomaitis"
      +"birth_date": null
      +"city": null
      +"height": null
      +"weight": null
    }
    1 => {#1332 ▶}
    2 => {#1323 ▶}
    3 => {#1333 ▶}
  ]
}

问题是当我尝试使用“{{$fact -> player-> name}}”查看玩家姓名时 当我收到这样的错误“未定义的属性:stdClass::$player(查看:.....”

但是当我得到这样的数据时:

$MatchFacts = Match_fact::where('match_id', $match->id)
        ->get();

没有问题,而且关系很好。 是否有任何选项可以使关系对我提到的问题起作用?

【问题讨论】:

    标签: arrays database laravel model-view-controller eloquent


    【解决方案1】:

    这是因为您直接通过查询构建器进行查询,而不是使用 Eloquent。这样,结果就不会像Match_fact 实例那样被水合,而是像普通的 stdClass 那样被水合。因此,没有在这些上定义 player 关系。

    要解决这个问题,请尝试使用 Eloquent 和关系查询您的数据

    $MatchFacts = Match_fact::with('player')
            ->where('match_id', $match->id)
            ->get();
    

    如果您已经正确定义了Match_fact 上的player 关系,那么您应该可以这样做:

    @foreach($MatchFacts as $fact)
      {{ $fact->player->name }}
    @endforeach
    

    【讨论】:

    • 使用此选项一切正常。是否有任何解决方案来获取玩家姓名: $homeTeamFacts = DB::table('match_facts') ->where('match_id', $match->id) ->join('players', 'match_facts.player_id', '=', 'players.id') ->where('players.team_id', '=', $homeTeamId) ->get();
    • 您可以尝试添加->select('match_facts.*', 'players.name')
    【解决方案2】:

    在您的情况下,您正在使用查询构建器从 DB DB::table('match_facts') 检索数据

    您可以使用查询生成器获取玩家姓名,只需将 select players.name 添加到您的 select() 方法中

    $homeTeamFacts = DB::table('match_facts')
        ->where('match_id', $match->id)
        ->join('players', 'match_facts.player_id', '=', 'players.id')
        ->where('players.team_id', '=', $homeTeamId)
        ->select('match_facts.*', 'players.name  as player_name')
        ->get();
    

    现在您可以使用$fact->player_name访问玩家名称

    但在我看来,使用模型和关系对于您的目的来说是更好的选择

    【讨论】:

      【解决方案3】:

      我认为,当您加入多个表时,意味着结果将不是该模型的实际实例,而只是一组数据,这就是关系不起作用的原因,您可以通过 add select in 获取玩家名称您将指定列是否可以将播放器的名称添加为选定的列,因此在数据集合中将是播放器的名称,也将作为 player_name

       $homeTeamFacts = DB::table('match_facts')->select([                DB::raw('players.name as player_name'),])
      
              ->where('match_id', $match->id)
              ->join('players', 'match_facts.player_id', '=', 'players.id')
              ->where('players.team_id', '=', $homeTeamId)
              ->get();
      

      【讨论】:

      • 也许你可以给个建议,如何通过加入获得玩家姓名?或者可能使用不同的方式来查询数据。
      猜你喜欢
      • 2022-01-12
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多