1- Create the Item:
php artisan make:model Item --all
2- Create the resource in web.php
:
Route::resource('items', ItemController::class);
3- Then, in the ItemController
's constructor, link the ItemPolicy
:
public function __construct()
{
$this->authorizeResource(Item::class);
}
4- Return true in all of the ItemPolicy
's methods (so you get authorized)
public function view(User $user, Item $item)
{
return true;
}
5- Add SoftDeletes
trait in Item
model:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Item extends Model
{
use HasFactory, SoftDeletes;
}
6- Add SoftDeletes
in Item migration. Run it.
public function up()
{
Schema::create('items', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->softDeletes();
});
}
7- Write a dd in show
method in ItemController
to check out if you are actually entering inside the method:
public function show(Item $item)
{
dd('You are inside the show function');
}
8- Create a item in the db and go to GET /items/1
. You should see the dd message.
Route::get('/testCreate', function () {
$item = Item::create();
return redirect("/items/$item->id");
});
9- Delete the item. Now, in GET /items/1
, the message doesn't appear anymore (instead, I get 404).
Route::get('/testDelete', function () {
$item = Item::firstOrFail();
$item->delete();
return redirect("/items/$item->id");
});
How to enter the show method in GET /items/1
despite the model is soft-deleted?
Notes
Make sure you are logged in
I already checked out this question but I'm not able to make it to work
I also tried to change the show method in the controller to this way ($id
instead of Item $item
), but anyways I get 404. I'm not entering the method, policy is in the middle and doesn't let me in:
public function show($id)
{
dd($id);
// dd($item);
}
It was as that easy as adding the withTrashed()
function at the end of the route resource route defined in routes/web.php
file.
Before:
Route::resource('items', ItemController::class);
After:
Route::resource('items', ItemController::class)->withTrashed(['show']);
In an older project I had, despite of doing the explained above, I was getting this error:
Method Illuminate\Routing\PendingResourceRegistration::withTrashed does not exist.
I solved it by updating the project's composer packages:
composer update
Now works fine.