Skip to content

Instantly share code, notes, and snippets.

@rodrigopedra
Last active October 24, 2019 21:41
Show Gist options
  • Save rodrigopedra/38ad5fbcf6adb44b014c to your computer and use it in GitHub Desktop.
Save rodrigopedra/38ad5fbcf6adb44b014c to your computer and use it in GitHub Desktop.

Revisions

  1. rodrigopedra revised this gist Apr 7, 2015. 1 changed file with 30 additions and 0 deletions.
    30 changes: 30 additions & 0 deletions Handler.php
    Original 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 );
    }
    }
  2. rodrigopedra revised this gist Apr 7, 2015. 1 changed file with 29 additions and 0 deletions.
    29 changes: 29 additions & 0 deletions 2015_04_07_204252_create_articles_table.php
    Original 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' );
    }
    }
  3. rodrigopedra created this gist Apr 7, 2015.
    13 changes: 13 additions & 0 deletions Article.php
    Original 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' );
    }
    }
    27 changes: 27 additions & 0 deletions ArticleController.php
    Original 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
    }
    28 changes: 28 additions & 0 deletions ArticleRequest.php
    Original 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');
    }
    }
    13 changes: 13 additions & 0 deletions routes.php
    Original 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',
    ]);