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 – Document Root Setup

DocRoot Setup

It is necessary to point main www address to Laravel’s /public folder, taking all the rest of Laravel framework files out of realm of web access.

DirectAdmin

You must have access to: Custom HTTPD Configurations
Once inside, click on: domain you have Laravel on.

option 1:

use available tokens

option 2:
you can try to add custom <Virtualhost> … something like:

… and add also similar code for SSL

option 3:

If you have access to server (Apache) itself, just go to

… and add code from option 2 directly to httpd-vhosts.conf file.

More can be found in DirectAdmin docs.

cPanel

option #1:
If you are on your own server and have access to httpd.conf file inside of /apache/conf/extra directory, you can change DocumentRoot in httpd.conf.
Read some more here in cPanel forums.

option #2:

If you cannot follow option 1, then do it differently, using cPanel interface.

You would need 2 domains:

  1. your main domain, you want your web page to show under
  2. some simple domain you dock cPanel on – it will be your main domain (as it is called in cPanel) – in reality, it will not.

Now, follow these steps:

  1. setup your cPanel with that docking domain as main.
  2. go to addon domains and create addon domain using your main domain (not docking one)
  3. when creating docking domain, set Document Root to something like:You may go to: Redirects and point your “docking” domain to Laravel Document Root

    note:

    At the end, there is a “public” folder.
    This is the same folder, you have all your public Laravel documents stored in.
    This is the ONLY folder, you should expose to web access.
    It is assumed, you install your Laravel to:

    Of course, if you intend to use your server for other domains, you may install Laravel to:

    Then, you should setup Document Root to:

    Some say, you should also redirect your Laravel domain to its http url in cPanel Redirects manager.

option 3:

If you have access to server (Apache) itself, just go to

… and add code from option 2 directly to httpd-vhosts.conf file.

Webmin/Virtualmin

option 1: (probably the easiest)

Select Virtualmin section.
Go to: Server Configuration / Website Options.
Chance setting in: Website documents sub-directory to public_html/public

option 2:

If you have access to server (Apache) itself, just go to

… and add code from option 2 directly to httpd-vhosts.conf file.

Localhost: XAMPP/WAMP

Look for:

Then, add code something like this code.

Then you should restart your WAMP/XAMPP.

 

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: