Add EarningPolicy
MODIFY
app/Http/Controllers/EarningsController.php
app/Http/Controllers/EarningsController.php
@@ -30,13 +30,11 @@ class EarningsController extends Controller {
return redirect()->route('dashboard');
}
- public function show($id) {
+ public function show(Earning $earning) {
$user = Auth::user();
$currency = $user->currency;
- $earning = Earning::find($id);
-
return view('earnings.show', compact('currency', 'earning'));
}
}

ADD
app/Policies/EarningPolicy.php
+<?php
+
+namespace App\Policies;
+
+use App\User;
+use App\Earning;
+
+class EarningPolicy {
+ public function view(User $user, Earning $earning) {
+ return $user->id === $earning->user_id;
+ }
+}

MODIFY
app/Providers/AuthServiceProvider.php
app/Providers/AuthServiceProvider.php
@@ -14,6 +14,7 @@ class AuthServiceProvider extends ServiceProvider
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
+ 'App\Earning' => 'App\Policies\EarningPolicy'
];
/**

MODIFY
routes/web.php
routes/web.php
@@ -15,7 +15,7 @@ Route::group(['middleware' => ['auth']], function () {
Route::get('/earnings/create', 'EarningsController@create')->name('earnings.create');
Route::post('/earnings', 'EarningsController@store');
- Route::get('/earnings/{id}', 'EarningsController@show')->name('earnings.show');
+ Route::get('/earnings/{earning}', 'EarningsController@show')->name('earnings.show')->middleware('can:view,earning');
Route::get('/spendings/create', 'SpendingsController@create')->name('spendings.create');
Route::post('/spendings', 'SpendingsController@store');

Add EarningPolicy