Posts Learn Components Snippets Categories Tags Tools About

How to Disable Mass Assignment For All Model in Laravel

Learn how to disable mass assignment globally for all model in Laravel the easy way

Created on Sep 13, 2021


Sometimes you might want to disable mass assignment protection in Laravel and to do that you have to call the "unguard()" method of the base Model class.
The above code should be called from the "boot" method of the "AppServiceProvider" class.

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
     * Bootstrap any application services.
     * @return void
    public function boot()
By having this "unguard" method called, you will have to make sure that any data that come into the Laravel be validated before storing it into the database.

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
    public function store(Request $request)
        $request->validate([/* validate your fields here */]);

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️


PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that are right for your project.

View Components

Sponsors 👑

+ Add Yours