Laravel 5.1: handle joins with same column names

前端 未结 4 1863
傲寒
傲寒 2021-02-04 00:00

I\'m trying to fetch following things from the database:

  • user name
  • user avatar_name
  • user avatar_filetype
  • complete conversation_messages<
4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 00:29

    Take a look at this example of trying to join three tables staffs, customers and bookings(pivot table).

     $bookings = \DB::table('bookings')
            ->join('staffs', 'staffs.id' , '=', 'bookings.staff_id')
            ->join('customers', 'customers.id' , '=', 'bookings.customer_id')
            ->select('bookings.id', 'bookings.start_time',  'bookings.end_time', 'bookings.service', 'staffs.name as Staff-Name',  'customers.name as Customer-Name')
            ->orderBy('customers.name', 'desc')
            ->get();
        return view('booking.index')
                ->with('bookings', $bookings);
    

提交回复
热议问题