· 7 years ago · Jan 09, 2019, 09:52 AM
1class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
2
3use Authenticatable, CanResetPassword;
4
5/**
6 * The database table used by the model.
7 *
8 * @var string
9 */
10protected $table = 'users';
11
12/**
13 * The attributes that are mass assignable.
14 *
15 * @var array
16 */
17protected $fillable = ['username', 'email', 'password'];
18
19/**
20 * The attributes excluded from the model's JSON form.
21 *
22 * @var array
23 */
24protected $hidden = [
25 'password',
26 'secret_key'
27];
28
29public function attribute()
30{
31 return $this->hasOne('AppUserAttributes', 'user_id', 'id');
32}
33
34public function photos()
35{
36 return $this->hasMany('AppPhotos', 'user_id', 'id');
37}
38
39public function favourites()
40{
41 return $this->hasMany('AppFavourites', 'from', 'id');
42}
43
44public function favourited()
45{
46 return $this->hasMany('AppFavourites', 'to', 'id');
47}
48
49class Favourites extends Model {
50
51 protected $table = 'favourite';
52
53 public function user()
54 {
55 return $this->belongsTo('AppUser', 'from', 'id');
56 }
57
58}
59
60class UserAttributes extends Model {
61
62 protected $table = 'user_attributes';
63
64 public function user()
65 {
66 return $this->belongsTo('User', 'user_id', 'id');
67 }
68}
69
70return User::with(['attribute','favourites'])->find($user_id);
71
72"favourites": [
73 {
74 "id": "15",
75 "from": "231", // My ID
76 "to": "**100**", // ID of user I have favourited (FK)
77 "created_date": "2013-04-10 21:35:28",
78 "user": [
79 "id": "**100**", // USER ID
80 "username": "someuser",
81 "email": "random@mail.com",
82 "email_verified": "1",
83 "has_photo": "1",
84 "dob": "1952-11-12"
85 ],
86 "attribute": [
87 "id": "105",
88 "user_id": "**100**", // USER ID (FK)
89 "height": "70",
90 "car": "0",
91 "pet": "2",
92 ]
93 }
94]