• 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 phpunit test failing authorization

How to generate QR CODE for dynamic generating link and logo using Simple QrCode in laravel?

How to set a domain name with php artisan serve

Trying to mock an http client that is inside a controller, using phpunit, but it doesn't work

file_put_content...fail to open stream:Permission denied in Laravel 5

How to send image,audio or video through the WhatsApp API - PHP

Laravel: Create morphs() relationship nullable

How to install php yaml on CentOs?

Wordpress wpdb->delete issue

Display Brand Name Above Product Title in Woocommerce Cart, Checkout Page, Orders and Email Notification

Change user role if checkout custom checkbox is checked in WooCommerce

Get number of working days between two dates in PHP [duplicate]

Python - Include another Python script

Submitting a form with ajax in Wordpress

How to reset Laravel AuthManager/guards in between API calls in tests?

About Contact Privacy policy Terms and conditions