Reputation: 17
I have an original request that I made in the Navicat program:
SELECT
tt1.team AS team1,
tt2.team AS team2,
tt1.logo AS team1_logo,
tt2.logo AS team2_logo,
tt1.id AS team1_id,
tt2.id AS `team2_id,`,
tournaments_matches.`status`,
tournaments_matches.started_at,
tt1.x AS x1,
tt2.x AS x2,
tournaments_matches.live,
tournaments_matches.winner_id
FROM
tournaments_teams AS tt1
INNER JOIN tournaments_teams AS tt2 ON tt1.match_id = tt2.match_id AND tt1.id != tt2.id
INNER JOIN tournaments_matches ON tournaments_matches.match_id = tt2.match_id AND tournaments_matches.match_id = tt1.match_id
GROUP BY
tt1.match_id
I tried to remake this query in Laravel Query Builder, I got this:
$opponents = DB::select('tt1.team as team1','tt2.team as team2','tt1.logo as team1_logo','tt2.logo as team2_logo','tt1.id as team1_id','tt2.id as team2_id,','tournaments_matches.status','tournaments_matches.started_at','tt1.x as x1','tt2.x as x2','tournaments_matches.live','tournaments_matches.winner_id')
->from('tournaments_teams as tt1')
->join('tournaments_teams as tt2', function($join) {
$join->on('tt1.match_id', '=', 'tt2.match_id')
->on('tt1.id', '!=', 'tt2.id');
})
->join('tournaments_matches', function($join) {
$join->on('tournaments_matches.match_id', '=', 'tt2.match_id')
->on('tournaments_matches.match_id', '=', 'tt1.match_id');
})
->groupBy('tt1.match_id')
->get();
var_dump($opponents); exit;
tournaments_teams
and tournaments_matches
it's a different tables in the database.
But when i try to access page, var_dump
say me error Type error: Argument 1 passed to Illuminate\Database\Connection::prepareBindings() must be of the type array, string given, called in
. Where is the problem? How i can fix this?
Upvotes: 0
Views: 43
Reputation: 476
Have a look at the documentation for selects: https://laravel.com/docs/7.x/queries#selects
I think your issue in selecting a table, try:
$opponents = DB::table('tournaments_teams as tt1')
->select('tt1.team as team1','tt2.team as team2','tt1.logo as team1_logo','tt2.logo as team2_logo','tt1.id as team1_id','tt2.id as team2_id,','tournaments_matches.status','tournaments_matches.started_at','tt1.x as x1','tt2.x as x2','tournaments_matches.live','tournaments_matches.winner_id')
->join('tournaments_teams as tt2', function($join) {
$join->on('tt1.match_id', '=', 'tt2.match_id')
->on('tt1.id', '!=', 'tt2.id');
})
->join('tournaments_matches', function($join) {
$join->on('tournaments_matches.match_id', '=', 'tt2.match_id')
->on('tournaments_matches.match_id', '=', 'tt1.match_id');
})
->groupBy('tt1.match_id')
->get();
Upvotes: 1