本文整理汇总了PHP中App\Model\User::firstOrNew方法的典型用法代码示例。如果您正苦于以下问题:PHP User::firstOrNew方法的具体用法?PHP User::firstOrNew怎么用?PHP User::firstOrNew使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类App\Model\User
的用法示例。
在下文中一共展示了User::firstOrNew方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: createOrFindUser
/**
* @param \Laravel\Socialite\Contracts\User $login
* @return User
*/
private function createOrFindUser($login)
{
$user = User::firstOrNew(['email' => $login->getEmail()]);
if ($user->exists) {
return $user;
}
$user->name = $login->getName() ?: $login->getNickname();
$user->nickname = $login->getNickname();
$user->password = Hash::make($login->getId() . time());
$user->avatar = $login->getAvatar();
$user->admin = User::count() == 0;
$user->save();
return $user;
}
示例2: profilePost
public function profilePost(Request $request, $username)
{
$user = User::firstOrNew(['username' => $username]);
$isOwn = $this->user && $this->user->id === $user->id;
$isAdmin = $this->user && $this->user->isAdmin();
if (!$isOwn && !$isAdmin) {
return redirect('users/' . $this->user->username);
}
$user->username = $request->input('username');
$user->name = $request->input('name');
$user->email = $request->input('email');
if ($request->input('password') !== $request->input('password_confirmation')) {
$this->alert('warning', 'Your passwords did not match. Not changed.');
} else {
$user->password = bcrypt($request->input('password'));
}
$user->save();
// Roles.
if ($isAdmin && $request->input('roles')) {
$user->roles()->sync($request->input('roles'));
}
// Save unavailabilities.
\DB::table('user_unavailabilities')->where('user_id', '=', $user->id)->delete();
foreach ($request->input('unavailabilities') as $unavail) {
if (empty($unavail['start_date']) && empty($unavail['end_date'])) {
continue;
}
$unavailability = new \App\Model\UserUnavailability();
$unavailability->user_id = $user->id;
$unavailability->start_date = $unavail['start_date'];
$unavailability->end_date = $unavail['end_date'];
$unavailability->save();
}
$this->alert('success', 'User profile information saved.');
return redirect('users');
}