Alen
Alen

Reputation: 1788

How to globally prevent saving to database in Laravel 5

I made a PHP script in Laravel and now I want to show demo to my buyers. Since I don't want them to make any changes to the database, I was wondering if there is a way to globally disable any saving to the database?

Upvotes: 2

Views: 1998

Answers (2)

user2094178
user2094178

Reputation: 9454

If you have a BaseModel that extends Laravel's default Eloquent model class. And all of your applications models extend that BaseModel you can add the following to it:

public static function boot()
{
    parent::boot();

    static::saving(function($model)
    {
        return false;
    });
}

This will intercept any creating or updating events from Eloquent.

This may be the technical answer for your question, but ideally just backup your db and restore it after showing to your buyers.

Upvotes: 5

baao
baao

Reputation: 73241

The easiest thing will be to create a mysql user that hasn't permissions to insert. Use the following mysql statement to create the user and use this user as the user in your database setting. I don't think there's a global method for this in laravel.

 GRANT SELECT ON [database name].[table name] TO ‘[username]’@'%’;

This way, your user can view everything, but isn't able to save a bit.

Upvotes: 3

Related Questions