How to user MySQL functions with eloquent and fluent

Both Eloquent ORM and Fluent query builder are wonderful tools, but, sometimes you want more, you may for example want to concatenate 2 fields (for presentation purposes Eloquent provides Getter & Setter Methods, but that’s another story for another time).

This:


 return self::get( array( 'id', 'CONCAT(first_name, " ", last_name)' ) );

Won’t work, to insert MySQL functions you have to use DB::raw() method:


 return self::get( array( 'id', DB::raw('CONCAT(first_name, " ", last_name)') ) );

Advertisements

One thought on “How to user MySQL functions with eloquent and fluent

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s