Last active
August 29, 2015 13:57
-
-
Save gravitano/9837287 to your computer and use it in GitHub Desktop.
Kalo mau find data via Model/Eloquent, mending pake `findOrFail` aja, nanti tinggal di try and catch, ini contohnya.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use Illuminate\Database\Eloquent\ModelNotFoundException; | |
| class UsersController extends Controller | |
| { | |
| /** | |
| * Remove the specified resource from storage. | |
| * | |
| * @param int $id | |
| * @return Response | |
| */ | |
| public function destroy($id) | |
| { | |
| try{ | |
| $user = User::findOrFail($id); | |
| $user->delete(); | |
| return Redirect::route('users.index') | |
| ->withFlashSuccess('That user has been deleted.') | |
| ; | |
| }catch(ModelNotFoundException $e) | |
| { | |
| return $this->redirectNotFound(); | |
| } | |
| } | |
| /** | |
| * Redirect because the model not found. | |
| * | |
| * @return Response | |
| */ | |
| protected function redirectNotFound() | |
| { | |
| return Redirect::route('users.index') | |
| ->withFlashError('That user does not exists.') | |
| ; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment