Last active
October 24, 2019 21:41
-
-
Save rodrigopedra/38ad5fbcf6adb44b014c to your computer and use it in GitHub Desktop.
Revisions
-
rodrigopedra revised this gist
Apr 7, 2015 . 1 changed file with 30 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ <?php namespace App\Exceptions; // app\Exceptions\Handler.php use Exception; use Illuminate\Database\Eloquent\ModelNotFoundException;// ADD THIS use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; class Handler extends ExceptionHandler { protected $dontReport = [ 'Symfony\Component\HttpKernel\Exception\HttpException' ]; public function report( Exception $e ) { return parent::report( $e ); } public function render( $request, Exception $e ) { // ADD THIS if ( $e instanceof ModelNotFoundException ) { return redirect()->back()->withInput()->withErrors( 'model not found' ); } return parent::render( $request, $e ); } } -
rodrigopedra revised this gist
Apr 7, 2015 . 1 changed file with 29 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ <?php // database/migrations/2015_04_07_204252_create_articles_table.php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateArticlesTable extends Migration { public function up() { Schema::create( 'articles', function ( Blueprint $table ) { $table->increments( 'id' ); $table->string( 'name' ); $table->integer( 'user_id' )->unsigned(); $table->timestamps(); $table->foreign( 'user_id' )->references( 'id' )->on( 'users' ); } ); } public function down() { Schema::drop( 'articles' ); } } -
rodrigopedra created this gist
Apr 7, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ <?php namespace App; // app/Article.php use Illuminate\Database\Eloquent\Model; class Article extends Model { public function user() { return $this->belongsTo( 'App\User' ); } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ <?php namespace App\Http\Controllers; // app/Http/Controllers/ArticleController.php use App\Article; use App\Http\Requests; use App\Http\Controllers\Controller; use Illuminate\Http\Request; class ArticleController extends Controller { public function __construct() { $this->middleware('auth'); // IMPORTANT! } public function edit( Requests\ArticleRequest $request, $articles ) { $article = Article::find($articles); return view('articles.edit', compact('article')); } // omitted for brevity } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ <?php namespace App\Http\Requests; // app/Http/Requests/ArticleRequest.php use App\Article; use App\Http\Requests\Request; class ArticleRequest extends Request { public function authorize() { $user = app( 'auth' )->user(); $article = Article::findOrFail( $this->articles ); // "articles" is a route parameter return $article->user_id === $user->id; } public function rules() { return []; } // optionally override this to redirect back public function forbiddenResponse() { return redirect()->back()->withInput()->withErrors('forbidden'); } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ <?php // app/Http/routes.php Route::resource( 'articles', 'ArticleController' ); Route::get('/', 'WelcomeController@index'); Route::get('home', 'HomeController@index'); Route::controllers([ 'auth' => 'Auth\AuthController', 'password' => 'Auth\PasswordController', ]);