postgresqllaravel-5eloquentlaravel-5.8composite-types

Laravel Eloquent Query builder accessing postgres composite column attributes


I'm using Laravel 5.8 and Postgres 11 / Postgis 2.5.2 and have a stdraddr column, which is a composite type. I can successfully run a query like this directly on the table:

select * from addresses where (address).house_num like '%2840%'

to access the columns within the address column.

I have yet to translate this query to Eloquent with the following attempts:

Address::where('address.house_num', 'LIKE', $houseNum)->get();

Address::where('(address).house_num', 'LIKE', $houseNum)->get();

Address::where('address[house_num]', 'LIKE', $houseNum)->get();

Address::where('address', 'LIKE', $houseNum)->get();

Address::where('house_num', 'LIKE', $houseNum)->get();

Address::whereRaw("(address).house_num LIKE {$houseNum}")->get();

and a few others that have thrown SQLSTATE exceptions. The $houseNum variable includes the wildcard characters. Has anyone used composite types with eloquent?

I've been able to just run a raw query: \DB::select('select * from addresses where (address).house_num LIKE ?', [$houseNum]);, but curious if Eloquent can do this?


Solution

  • Use whereRaw() with the code and binding from your raw query:

    Address::whereRaw('(address).house_num LIKE ?', [$houseNum])->get();