-2

Might be stupid question, how to correctly state the ID in select statement?

public function generatefinalconfirmation($id) {
        $booking = Booking::findOrFail($id);
        $flightin = DB::select('SELECT b.bookingname, f.origin FROM Bookings B JOIN Flights F ON B.Flightin = F.FlightId WHERE B.ID = id');
}

That code give me all the data while where b.id = $id give me an error message.

Thanks

Edit: Thanks all for the answers, sorry for all the rookie mistake

doom
  • 11
  • 1
  • 3

1 Answers1

3

This way you prevent SQL injections and make your code more readable:

public function generatefinalconfirmation($id) {
        $booking = Booking::findOrFail($id);
        $flightin = DB::table('Bookings AS B')
                ->select([ 'b.bookingname', 'f.origin'])
                ->join('Flights AS F', 'B.Flightin', 'F.FlightId')
                ->where('B.ID', $id);
}
shaedrich
  • 5,457
  • 3
  • 26
  • 42
  • 2
    Might need the `as` keyword in there for the aliases. I've seen QueryBuilder be picky about that. – aynber Jul 16 '21 at 12:58