fix getting user from UserRepository by custom property

This commit is contained in:
printempw 2017-01-08 10:40:22 +08:00
parent f5dd7c8c5f
commit 4276b56e0c
2 changed files with 14 additions and 3 deletions

View File

@ -12,7 +12,7 @@ class HomeController extends Controller
{ {
public function index(UserRepository $users, Request $request) public function index(UserRepository $users, Request $request)
{ {
return view('index')->with('user', $users->get(session('uid'))); return view('index')->with('user', $users->getCurrentUser());
} }
public function locale($lang, Request $request) public function locale($lang, Request $request)

View File

@ -20,7 +20,7 @@ class UserRepository extends Repository
if ($type == "uid") { if ($type == "uid") {
return Arr::has($this->items, $identification); return Arr::has($this->items, $identification);
} else { } else {
return Arr::where((array) $this->items, function($key, $value) use ($identification, $type) { return (bool) Arr::where((array) $this->items, function($key, $value) use ($identification, $type) {
if (property_exists($value, $type)) if (property_exists($value, $type))
return false; return false;
@ -56,9 +56,20 @@ class UserRepository extends Repository
$this->set($user->uid, $user); $this->set($user->uid, $user);
return $user; return $user;
} }
return null;
} }
return Arr::get($this->items, $identification, null); $result = Arr::where((array) $this->items, function($key, $value) use ($identification, $type) {
if (property_exists($value, $type))
return false;
return ($value->$type == $identification);
});
// return first element
reset($result);
return current($result);
} }
public function getCurrentUser() public function getCurrentUser()