• 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

Is it possible to re-use an array taken from PHP via JSON in a $.ajax function?

Override default Auth routes in Laravel 7

Laravel Nova Dependency Container what are the allowed resource properties other than id in dependsOn

How do I enable error reporting in Laravel?

Search for array row with specific column value and return another value from qualifying row

How to redirect to another page after n seconds in wordpress without using javascript?

How to insert Google Adsense Code in PHP script?

Set quantity minimum, maximum and step at product level in Woocommerce

Two buttons one form

How to Create WooCommerce Subscription Product via. REST API?

virtctl works when executed via command line but not from php exec()

Symfony Mercure "Failed to send an update"

Vimeo API: how to save a vimeo into a subfolder?

CSV to PHP class properties

Filter array by skipping every nth element from the end

About Contact Privacy policy Terms and conditions