本文整理汇总了PHP中App\Model\User::findOrFail方法的典型用法代码示例。如果您正苦于以下问题:PHP User::findOrFail方法的具体用法?PHP User::findOrFail怎么用?PHP User::findOrFail使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类App\Model\User
的用法示例。
在下文中一共展示了User::findOrFail方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: deleteUser
/**
* Finds a user and soft deletes it
*
* @param int $userId
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*
* @return null
*/
public static function deleteUser($userId)
{
$user = User::findOrFail($userId);
if ($user) {
$user->delete();
} else {
throw \Illuminate\Database\Eloquent\ModelNotFoundException;
}
}
示例2: dispatch
public function dispatch(Request $request, Response $response, $args)
{
$user = $this->container->get('currentUser');
$id = isset($args['id']) ? (int) $args['id'] : null;
$notes = [];
if ($id) {
$note = Note::where('id', $id)->where('user_id', $user->userId)->firstOrFail();
$notes = [$note];
} else {
$notes = User::findOrFail($user->userId)->notes;
}
return $response->write(json_encode(['message' => 'List of Notes', 'data' => $notes]));
}
示例3: destroy
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
/**
* Thực hiện tìm và xóa TK theo id truyền vào
* Nếu lỗi chuyển hướng
*/
try {
$user = User::findOrFail($id)->delete();
return redirect('admin/user-manager');
} catch (Exception $e) {
return abort(404);
}
}
示例4: update
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param int $id
* @return Response
*/
public function update(Request $request, $id)
{
$from = $request->input('from', '');
$to = $request->input('to', '');
if (empty($id) || $from === '' || $to === '') {
return new Response("Mandatory parameter(s) are missing.", 405);
}
try {
if ($id > 0) {
$user = User::findOrFail($id);
$old_desk = $user->desk;
} else {
if ($id < 0) {
// Busy Flag
$old_desk = Desk::find($from);
} else {
return new Response("Invalid user id.", 405);
}
}
#print_r( $old_desk->toArray() );
if ($old_desk != null && $from != $old_desk->id) {
return new Response("Out of sync, user was moved someplace else already.", 409);
}
if ($to != 0) {
$new_desk = Desk::findOrFail($to);
if (!$this->checkEmptyDesk($new_desk)) {
return new Response("Out of sync, target desk is not empty.", 409);
}
} else {
$id = 0;
}
$ret = [];
if ($old_desk != null) {
$old_desk->users_id = 0;
$old_desk->save();
$ret['old'] = $old_desk->toArray();
}
if (isset($new_desk)) {
$new_desk->users_id = intval($id);
$new_desk->save();
$ret['new'] = $new_desk->toArray();
}
return response()->json($ret);
} catch (\Exception $e) {
return new Response("There was an error while processing the update request: " . $e->getMessage(), 500);
}
}
示例5: destroy
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
$user = User::findOrFail($id);
$user->delete();
return redirect()->action('UserController@index');
}