Share this
Illuminate\Database\QueryException
SQLSTATE[23000]: Integrity constraint violation: 1052 Column ‘created_at’
I am getting this error when i am trying to join three tables in following code and the error is SQLSTATE[23000]: Integrity constraint violation: 1052 Column ‘created_at’ which i am getting when i try to run the project.
return Order::leftJoin('order_items', 'orders.id', '=', 'order_items.order_id') ->join('users', 'orders.user_id', '=', 'users.id') ->join('attendees', 'orders.id', '=', 'attendees.order_id') ->select('orders.id','orders.name','orders.order_amount','orders.billing_f_name','orders.billing_l_name','orders.billing_email', 'users.email as user_email','order_items.course_name as course_name','attendees.first_name as attendee_first_name') ->latest() ->get();
Solution
in the above program you are try to get latest records by multiple ‘created_at’ fields because your all tables have ‘created_at’ field so you need to short record by one filed you can specify table name with column name in latest function which is following
->latest('orders.created_at')
Ravindra is a passionate full stack developer and dedicated blogger with a flair for crafting user-friendly web applications and insightful articles. With expertise spanning front-end and back-end technologies, Ravindra brings ideas to life through innovative coding solutions.