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.
Laravel - Edit Article only if user is the owner
<?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' );
}
}
<?php namespace App;
// app/Article.php
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
public function user()
{
return $this->belongsTo( 'App\User' );
}
}
<?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
}
<?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');
}
}
<?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 );
}
}
<?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',
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment