• Categories
    • PHP
    • phpMyAdmin
    • PHPMailer
    • FFMpeg
    • PHPEXcel
    • PHPDoc
    • PHPUnit
    • CakePHP
    • CakePHP 2.0
    • Cake PHP 2.1
    • CakePHP Model
    • Facebook PHP SDK
    • composer-php
    • PHP 7
    • PHP GD
    All Categories
  • About

Laravel - Implicit route model binding with soft deleted data

phplaravelrouteslaravel-5.2


PHP Snippet 1:

//Route Service Provider
 $router->bind('post', function($post)
     return Post::withTrashed()->where('id', $post)->firstOrFail();
});

// Controller
public function show(Post $post) {

// If the post has been trashed and the user is not admin, he cannot see it
     if (!Auth::user()->isAdmin() && $post->trashed())
         abort(404);

     // Proceed with normal request because the post has not been deleted.
}

PHP Snippet 2:

Route::get('posts/{post}', [PostController::class, 'show'])->withTrashed();

Related Snippets

RuntimeException: Unable to create the cache directory (/var/www/sonata/app/cache/dev)

Utf8 encoding issue with Laravel

Laravel multi auth - Authentication user provider [] is not defined

Login if user is active using Laravel Breeze

PHP SoapClient: set a namespace without prefix

PHP random string generator

How do I upload a laravel project on cPanel shared hosting?

Laravel 8 factory state afterCreating

How avoid Moved Permanently The document has moved here

Xdebug can't connect back to Docker host

Currently Using CodeIgniter Framework i have an Error

Twig - How to check if variable is a number / integer

I want to use codeigniter foreign character library in my custom PHP project how i can use it?

Failed user login on production server using Symfony framework (Authentication request could not be processed due to...)

Laravel Nova, route not found

About Contact Privacy policy Terms and conditions