Try adding the properties.updated_at to the select.
Jul 6, 2023
7
Level 1
Not able to use orderby with distinct in laravel Query
Hi all, I am using Laravel 10. Given below is my Laravel query.
$property = DB::table('properties')
->leftjoin('property_sub_types', 'property_sub_types.id', '=', 'properties.property_sub_type')
->leftjoin('property_images', 'properties.id', '=', 'property_images.property_id')
->leftjoin('cities', 'cities.id', '=', 'properties.city')
->select('properties.name as proName', 'properties.id as pro_id', 'properties.city', 'properties.street', 'property_sub_types.name as subName', 'property_images.image as imgName','cities.name as ctyName','properties.reserve_price','properties.description','properties.possession','properties.ownership','properties.auction_start_date','properties.contact_manager','properties.contact_number')
->where('properties.property_status', 4)
->where('properties.status', 1)
->where('property_sub_types.status', 1)
->distinct('properties.id')
->paginate(30);
When I use
->orderBy('properties.updated_at', 'desc')
in query showing error
SQLSTATE[42P10]: Invalid column reference: 7 ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions LINE 1: select distinct on ("properties"."id") "properties"."name" a... ^ (Connection: pgsql, SQL: select distinct on ("properties"."id") "properties"."name" as "proName", "properties"."id" as "pro_id", "properties"."city", "properties"."street", "property_sub_types"."name" as "subName", "property_images"."image" as "imgName", "cities"."name" as "ctyName", "properties"."reserve_price", "properties"."description", "properties"."possession", "properties"."ownership", "properties"."auction_start_date", "properties"."contact_manager", "properties"."contact_number" from "properties" left join "property_sub_types" on "property_sub_types"."id" = "properties"."property_sub_type" left join "property_images" on "properties"."id" = "property_images"."property_id" left join "cities" on "cities"."id" = "properties"."city" where "properties"."property_status" = 4 and "properties"."status" = 1 and "property_sub_types"."status" = 1 order by "properties"."updated_at" desc limit 30 offset 0)
Please or to participate in this conversation.