Skip to content

Instantly share code, notes, and snippets.

@gravitano
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save gravitano/9837287 to your computer and use it in GitHub Desktop.

Select an option

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.
<?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