Doubt about eloquent with Laravel

Asked

Viewed 49 times

0

Eai guys, I’m setting up a management system of football clubs just to practice and in that came a question, I have the following eloquent:

$matches = DB::table('ut_tickets AS TIC')
    ->select('TIC.id', 'STA.name AS stadium', 'CLM.name AS club_main', 'CLV.name AS club_visitor', 'CLM.arms AS arms_main', 'CLV.arms AS arms_visitor', 'MAT.date_match', 'MAT.schedule_match', 'CHAMP.name AS championship')
    ->join('ut_lots AS LOT', 'LOT.id', 'TIC.id_lot')
    ->join('ut_matches AS MAT', 'MAT.id', 'LOT.id_match')
    ->join('ut_stadiums AS STA', 'STA.id', 'MAT.id_stadium')
    ->join('ut_clubs AS CLM', 'CLM.id', 'MAT.id_club_main')
    ->join('ut_clubs AS CLV', 'CLV.id', 'MAT.id_club_visitor')
    ->join('ut_championships AS CHAMP', 'CHAMP.id', 'MAT.id_championship')
    ->join('ut_transactions AS TRAN', 'TRAN.id', 'TIC.id_transaction')
    ->join('users AS USR', 'USR.id', 'TRAN.id_user')

    ->where('TRAN.id_user', Auth::user()->id)
    ->groupBy('MAT.id')->orderBy('MAT.date_match', 'DESC')->get();

dd($matches);

dd is returning me to Collection:

Collection {#397 ▼
  #items: array:1 [▼
    0 => {#384 ▼
      +"id": 1
      +"stadium": "Beira Rio"
      +"club_main": "Internacional"
      +"club_visitor": "Grêmio"
      +"arms_main": "/images/clubs/1/1537880299212.png"
      +"arms_visitor": "/images/clubs/2/1537880338132.png"
      +"date_match": "2018-12-21"
      +"schedule_match": "16:00:00"
      +"championship": "Gauchão 2018"
    }
  ]
}

Only that in my front-end I intend to customize as the user attended the game, if it is, display the die in one way, if not another.

But the way I put it together, it only comes back if you have a ticket purchased from that customer. Is it possible for me to bring all the data even if the user did not buy the ticket? If yes, how would I have to assemble this Eloquent to bring the frequented and not frequented (The way I thought was according to the ticket "ticket" if there is a record of id_user on the table ut_tickets is because he bought, I just don’t know how to treat the result for when he doesn’t buy :x

  • try leftJoin()

  • It would help if you shared the tables you’re using :) Anyway, try trading Join for leftJoin. Explanation: - "Join" will return results that are in both tables, that is, people who are not in one of the tables will not appear. - With leftjoin, all records from the first table and the records from the second table that correspond to the first are returned. - You still have the rightjoin that does the same as the left, but the other way around. I hope I helped.

  • 1

    Read: https://answall.com/questions/348273/com-left-join-listar-os-not-in/348286#348286

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.