php - Laravel Repository eagle load pre defined accessor -


just wanted know if possible eager load define accessor in laravel repository. using l5-repository package , laravel 5.4.

in userinfo.php model, have function

public function getfullnameattribute() {     return ucfirst($this->first_name . " ". $this->last_name); } 

then trying eager load accessor in controller this.

$user_infos = $this->repository->with('full_name')->all();  // or $user_infos = $this->repository->with('getfullnameattribute')->all(); return response()->json([ 'data' => $user_infos ]); 

i know not gonna work. looking similar way add accessor full_name in collection. don't need concatenate in front end. expected result

{    id : 1,    first_name : "sample fname",    last_name : "sample lname",    .....    .....    full_name : "sample fname sample lname", } 

use $appends in model userinfo.php

protected $appends = ['full_name']; 

this append custom field in collection.

then can see here :

$user_infos = userinfo::all();  $user_infos->tojson(); 

you can see appended accessor in json.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -