0

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?

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

whoops. it was my mistake, now all work, thank you! UPD: but now, when i remove var_dump i received error Invalid argument supplied for foreach() :(
Great, please consider marking this as the answer to your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.