vendredi 11 octobre 2019

Cannot get the model attibute using eager load in Laravel Eloquent relation

Model attributes not accessible when using Laravel eager loading. Previously I had used laravel 5.4 so when I try to eager load and we load teamparticipantone and teamparticipanttwo or playeroneparticipantone and playeroneparticipanttwo base on tournament type using below it worked:

$tournament->matches->load('teamparticipantone', 'teamparticipanttwo');

But, I have recently updated my project laravel version from 5.4 to 5.7 then it throws error tournament_id is null. $tournament->matches->load('teamparticipantone', 'teamparticipanttwo');

But, I have recently updated my project laravel version from 5.4 to 5.7 then it throws error tournament_id is null.

How I can solve this issue or change the relationship structure or I want to set morph but, morph for tournament_matches table and his morph type base on parent table tournaments.

How can I achieve this? How can I solve this issue with a better solution?

After doing a lot of debugging and research I found that after the version upgrade the below file has changed.

File Name: \www\html\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php Function Name: getRelation Line Number: 584 In try block

In laravel 5.4 it return: return $this->getModel()->{$name}();

But in laravel 5.7 it return: return $this->getModel()->newInstance()->$name();

I have below DB table structures:

 users Table
 -id (PK)
 -email (UK)


 teams Table
 -id (PK)
 -team_name (UK)


 tournaments Table
 -id (PK)
 -title
 -type (1 = Team, 2 = User)


 tournament_matches Table
 -id (PK)
 -tournament_id (FK of tournaments.id)
 -participant_one (team_id if tournament_type=1 OR user_id if tournament_type=2)
 -participant_two (team_id if tournament_type=1 OR user_id if tournament_type=2)
 -participant_one_score
 -participant_two_score


 tournament_teams Table
 -tournament_id (FK of tournaments.id)
 -team_id (FK of teams.id)


 tournament_users Table
 -tournament_id (FK of tournaments.id)
 -user_id (FK of users.id)

Laravel Eloquent: In Match Model of tournament_matches table: Match.php

public function teamparticipantone()
{
 return $this->belongsTo('Models\Team', 'participant_one', 'id')->withTrashed()
    ->leftJoin('tournament_teams', function ($join) {
        $join->on('tournament_teams.team_id', '=', 'teams.id');
        $join->on('tournament_teams.tournament_id', '=', DB::raw($this->tournament_id));
    });
}

public function teamparticipanttwo()
{
  return $this->belongsTo('Models\Team', 'participant_two', 'id')->withTrashed()
    ->leftJoin('tournament_teams', function ($join) {
        $join->on('tournament_teams.team_id', '=', 'teams.id');
        $join->on('tournament_teams.tournament_id', '=', DB::raw($this->tournament_id));
    });
}

public function playeroneparticipantone()
{
 return $this->belongsTo('Models\User', 'participant_one', 'id')
->join('tournament_users', 'tournament_users.user_id', '=', 'users.id')
    ->where('tournament_users.tournament_id', $this->tournament_id);
}

public function playeroneparticipanttwo()
{
 return $this->belongsTo('Models\User', 'participant_two', 'id')
->join('tournament_users', 'tournament_users.user_id', '=', 'users.id')
    ->where('tournament_users.tournament_id', $this->tournament_id);
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire