• 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

SQLSTATE[HY000]: General error: 1 table posts has no column named *

WordPress ACFNotice: get_field() - We've detected one or more calls to retrieve ACF field values before ACF has been initialized

SlimExceptionHttpNotFoundException

Login if user is active using Laravel Breeze

Lexik JWT authentication problem "Invalid credentials"

Target Individual Form Instance/Counter Inside A PHP While Loop

How to get total pages of PDF with FPDF?

Increase value by 1 on button click

Not able to access model in the controller mautic

Laravel Excel::store file empty after stored

PHP only Hit Counter?

Elasticsearch - Want to sort by field in all indices where that particular field available or not if not then avoid it

How to add automatically collapse/expand in content wordpress (single.php)?

Invalid value in field "itemtype" in Google Search Console

best way to store error messages in Laravel (session or variable)

About Contact Privacy policy Terms and conditions