php - Laravel API - Get list of users I have favourited and their information -


i building api in laravel 5 , want list of people have favourited , information.

user model

class user extends model implements authenticatablecontract, canresetpasswordcontract {  use authenticatable, canresetpassword;  /**  * database table used model.  *  * @var string  */ protected $table = 'users';  /**  * attributes mass assignable.  *  * @var array  */ protected $fillable = ['username', 'email', 'password'];  /**  * attributes excluded model's json form.  *  * @var array  */ protected $hidden = [     'password',         'secret_key' ];  public function attribute() {     return $this->hasone('app\userattributes', 'user_id', 'id'); }  public function photos() {     return $this->hasmany('app\photos', 'user_id', 'id'); }  public function favourites() {     return $this->hasmany('app\favourites', 'from', 'id'); }  public function favourited() {     return $this->hasmany('app\favourites', 'to', 'id'); } 

favourites model

class favourites extends model {     protected $table = 'favourite';     public function user()    {        return $this->belongsto('app\user', 'from', 'id');     }  } 

user attributes model

class userattributes extends model {      protected $table = 'user_attributes';      public function user()     {        return $this->belongsto('user', 'user_id', 'id');     } } 
  • user table holds information username, email password etc.
  • attribute table holds information height, weight, etc. (this user's table doesn't have 70 columns)
  • favourite table holds , columns. from user id , to user id have fvaourited.

this have:

return user::with(['attribute','favourites'])->find($user_id); 

what want achieve similar below:

"favourites": [     {         "id": "15",         "from": "231", // id         "to": "**100**", // id of user have favourited (fk)         "created_date": "2013-04-10 21:35:28",         "user": [             "id": "**100**", // user id             "username": "someuser",             "email": "random@mail.com",             "email_verified": "1",             "has_photo": "1",             "dob": "1952-11-12"         ],         "attribute": [             "id": "105",             "user_id": "**100**", // user id (fk)             "height": "70",             "car": "0",             "pet": "2",         ]     } ] 

i need json above when call api url /api/v1/favourites?my_id=231 in app or site , required information rather loading individually. unless wrong?

if think way of doing wrong app, more happy hear input new me please bare me :-)

you need transform data before send it. may want create function or class 'usertransformer' you. fractal may you. incase you're laracasts fan or new laravel, have missed incremental api series (https://laracasts.com/series/incremental-api-development).


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -