• 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

Laravel csrf token mismatch for ajax POST Request

Populate Dynamic Dropdowns List in Codeigniter

target [LaravelFortifyContractsRegisterViewResponse] is not instantiable

Script @php artisan package:discover handling the post-autoload-dump event returned with error code 255

How to delete old images from public folder on update using Laravel

Magento 2 - How to add new block to admin panel in product page?

PHP array, move keys and values to new array, but mix it up

Redirect to previous page with php

How to properly set "0000-00-00 00:00:00" as a DateTime in PHP

Limit WooCommerce products in cart only from one custom taxonomy

Laravel display validation error

Laravel update hasMany relationship using saveMany

Normalize DateInterval in PHP

PHP - installing Xdebug on Mac with XAMPP (Unix File)

GuzzleHttp Hangs When Using Localhost

About Contact Privacy policy Terms and conditions