monteecristoo’s blog

https://twitter.com/irakaarozo

tinker

$follows = App\Follow::where('user_id', 4)->get()
=> Illuminate\Database\Eloquent\Collection {#727
all: [
App\Follow {#750
id: 1,
user_id: 4,
target_id: 1,
created_at: "2017-05-30 11:44:03",
updated_at: "2017-05-30 11:44:03",
},
App\Follow {#749
id: 2,
user_id: 4,
target_id: 2,
created_at: "2017-05-30 11:44:04",
updated_at: "2017-05-30 11:44:04",
},
],
}

 


>>> foreach($follows as $follow) { return $follow->user()->get(); }
=> Illuminate\Database\Eloquent\Collection {#738
all: [
App\User {#728
id: 4,
name: "Akari1987",
email: "",
provider: "github",
provider_id: "13121500",
avatar: "1496292872.jpg",
created_at: "2017-05-28 11:05:58",
updated_at: "2017-06-01 04:54:32",
},
],
}

-=======================================================

>>> $friends = array()
=> []
>>> $follows = App\Follow::where('user_id', 4)->get()
=> Illuminate\Database\Eloquent\Collection {#717
all: [
App\Follow {#718
id: 4,
user_id: 4,
target_id: 3,
created_at: "2017-06-10 23:26:25",
updated_at: "2017-06-10 23:26:25",
},
App\Follow {#719
id: 5,
user_id: 4,
target_id: 2,
created_at: "2017-06-10 23:29:07",
updated_at: "2017-06-10 23:29:07",
},
App\Follow {#720
id: 6,
user_id: 4,
target_id: 1,
created_at: "2017-06-10 23:29:10",
updated_at: "2017-06-10 23:29:10",
},
],
}
>>> foreach($follows as $follow) {
... array_push($friends, App\User::find($follow->target_id));
... }
=> null

>>> return $friends
=> [
App\User {#727
id: 3,
name: "test3",
email: "test3@test.com",
provider: null,
provider_id: null,
avatar: "1495604118.jpg",
created_at: "2017-06-09 22:41:08",
updated_at: "2017-06-09 22:41:08",
},
App\User {#723
id: 2,
name: "test2",
email: "test2@test.com",
provider: null,
provider_id: null,
avatar: "1495604947.jpg",
created_at: "2017-06-09 22:41:08",
updated_at: "2017-06-09 22:41:08",
},
App\User {#726
id: 1,
name: "test",
email: "test@test.com",
provider: null,
provider_id: null,
avatar: "1495699904.jpg",
created_at: "2017-06-09 22:41:08",
updated_at: "2017-06-09 22:41:08",
},
]