Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

php - How to check if related table doesn't have record

matches table has releationship with scores table.

I need to get all matches without record in scores table.

tryied smth, but didint worked:

$upcomingMatch = Match::join('scores', 'matches.id', '=', 'scores.match_id')
           ->where('away_team_id', '=', '1')
           ->orWhere('home_team_id', '=', '1')
           ->where('scores.match_id', null)
           ->latest('matches.match_date')
           ->first();
question from:https://stackoverflow.com/questions/65923882/how-to-check-if-related-table-doesnt-have-record

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you use relationship, you can use doesntHave() , but if you use query builder as you write in your question, you may use these codes, I saw that your where condition need some adjustment to use nested condition. If you want to retrieve null record, then use whereNull in scores table which is not a relationship key with matches table, in this example I use scores.id

Get matches which its score is null (use leftJoin with some additional condition)

$upcomingMatch = Match::leftjoin('scores', 'matches.id', '=', 'scores.match_id')
           ->where(function ($where)
           {
               $where->where('matches.away_team_id', '=', '1')
               ->orWhere('matches.home_team_id', '=', '1');
           })
           ->whereNull('scores.id')
           ->latest('matches.match_date')
           ->first();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...