Laravel 5: Gotchas – Global Scope

Global Scope with update and select, when using whereHas and related model

When you do any updated (and possibly inserts) you have to call withoutGlobalScope before find():

make private

If you move withoutGlobalScope after find, when you attempt to make aid private, you will make all aids private.

All of them.

Again, I did not investigated why (no time at the moment), but this is how it works.


If you move withoutGlobalScope after find, you will get:

This is because this particular scope filers out all ‘private’ aids.

Bottom line is that, when you release global scope, it must come before using find() method.

Now interesting thing below.

However, when you use Global Scope release (withoutGlobalScope) in select, especially when calling related model and you will use whereHas() method, you need to use it after find() method and after calling related model, or whereHas() method will not work.

See sample code:

So, usually order is not that important in Laravel Query Builder. However, when Global Scopes comes to play, it looks like it becomes important.

Anyway, this is a practical angle.
Tested on L5.6

Laravel 5: Configuration: database based configuration

Laravel 5: Configuration: database based configuration

Laravel offers great, file based configuration, but it is cumbersome to adjust from GUI.

GUI friendly database based configuration.
Database tables
I use 3 tables to accomplish database based config.
  1. configurations
    This table holds main configuration key/value pairs, plus some extra stuff.
  2. configuration_groups
    This table is just in case, if you get to have a lot of configuration data and want to group them in some GUI.
  3. configurables
    If you have many user types, with different access levels, you may use this to link particular configuration key/value to user. Since this is a poly-pivot table, you can easily clone multiple groups at any time of your app development. Of course this is meant for GUI use, if you want to allow editing of db based config values.
Migration Files



Language Files

Columns: ‘name’, ‘description’ do not hold actual names, or description, but location to language localized translations.

Here are my language files.




If you group your files in subfolders, dot notation would not work. You have to use slashes, e.g.:

Service Provider code

I used existing service provider: AppServiceProvider, to fire my database based config values.
See method: dbConfig();

I am using cache, to make it speedy.
My cache never expires.
Reason for this forever cache is that I want it to hold, until it is changed, e.g. from GUI.
It will be (should be) overwritten, when anything is changed va GUI.

How to Use It

You use it as you’d use standard config stuff.

I added ‘db.‘ prefix, to make it clearly visible in code, where it comes from.
No need to add it to db config key.

That is all.


Laravel 5: Startup – Configuration / Maintenance

Pretty much everything is clearly explained here.

But lets take a look some pointers, that should be pulled up, to let see things clearer.

You must remember to setup DATABASE access data in .env file.

For code completion you may use this file. Place it in Laravel app root folder, Do not push it to repository!

.env (in main catalog) file overrides information stored in your config files (folder: /config).
It is great for development purposes – just make sure you will not upload it to repo, or download any from repo (e.g. put there by some team member by mistake).

If you get this error in CLI:

… you forgot to edit your Database credentials in your .env file.



  1. all config files are stored in /config dir
  2. accessing config value anywhere within Laravel powered app:

    app‘ is a config file name, and
    timezone‘ in config value
  3. setting config values at runtime (not in config file, but ad hoc):
  4. .env: startup configuration values (environment configuration)
    all .env values are loaded to PHP superglobal $_ENV
    env helper function is used to load to $_ENV, e.g. see config files
    env helper is used with default value, in case .env file does not have one, e.g.:

    value in .env file overrides helper function default file (false in given example)
    do not push to remository .env file, as your local devel configuration on .env may not suit online project
  5. configuration files catching:
    should be done for production ready version
    should NOT be used during development routine
    code (in terminal):



If you want to put your site in a stand-by, use this code:

If you want it live again, use this code: