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.

makepublic

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: 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.

 

Configuration

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

    where:
    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):

 


Maintenance

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

If you want it live again, use this code: