Атрибуты модели недоступны при активной загрузке Laravel. Ранее я использовал laravel 5.4, поэтому, когда я пытаюсь выполнить загрузку, и мы загружаем teamparticipantone и teamparticipanttwo или playeroneparticipantone и playeroneparticipanttwo на основе типа турнира, используя приведенное ниже:
$tournament->matches->load('teamparticipantone', 'teamparticipanttwo');
Но я недавно обновил свой проект laravelверсия с 5,4 до 5,7, затем выдает ошибку: tournament_id is null
.
$tournament->matches->load('teamparticipantone', 'teamparticipanttwo');
Но я недавно обновил версию моего проекта Laravel с 5,4 до 5,7, после чего выдается сообщение об ошибке tourinid: null.
Как я могу решить эту проблему или изменить структуру отношений, или я хочу установить morph но, morph для таблицы tournament_matches
и его базу типов morph на родительской таблице tournaments
.
Как мне этого добиться? Как решить эту проблему с помощью лучшего решения?
После долгих отладок и исследований я обнаружил, что после обновления версии изменился приведенный ниже файл.
Имя файла: \ www \html \ vendor \ laravel \ framework \ src \ Illuminate \ Database \ Eloquent \ Builder.php Имя функции: номер строки getRelation: 584 В блоке try
В laravel 5.4 он возвращает: return $ this-> getModel ()-> {$ name} ();
Но в laravel 5.7 он возвращает: return $ this-> getModel () -> newInstance () -> $ name ();
У меня естьниже структуры таблиц БД:
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: в Match Model tournament_matches
таблица: 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);
}