How to use an accessor in Laravel 4 that only affects a field in a certain query function -


is there way use accessor change field query in laravel 4? so, have model, "fanartist.php". in table, "artists", have field "description". calling join in query function. function:

public static function fan_likes_json() {         $fan_likes = db::table('fanartists')                     ->join('artists', 'fanartists.artist_id', '=', 'artists.id')                     ->where('fanartists.fan_id', '=', auth::user()->id)                     ->select('artists.id', 'artists.stage_name', 'artists.city', 'artists.state', 'artists.image_path', 'artists.description')                     ->get();    } 

i trying change "description" string specific query function, using this:

public function getdescriptionattribute($value) {          return substr($value, 0, 90);      } 

do add fanartist model, or artist model? , also, how have accessor affect description field specific query function? description remain unaltered other purposes , queries. thank help.

i don't think eloquent's accessors/mutators custom query 1 you've specified. alternative, try this:

$fan_likes = db::table('fanartists')     ->join('artists', 'fanartists.artist_id', '=', 'artists.id')     ->where('fanartists.fan_id', '=', auth::user()->id)     ->select(db::raw('substring(artists.description, 1, 90) description, artists.id, artists.stage_name, artists.city, artists.state, artists.image_path'))     ->get(); 

then, purpose of query, artist description shortened version.

see: mysql substring , raw expressions in laravel 4


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -