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:

 

Laravel 5: Startup – Installation (Laravel 5 on XAMPP 3.2.2 / PHP 7.0.1)

STEP 1: Install Composer

Download Composer from here: http://getcomposer.org/download/
Install Composer.


STEP 2: Install XAMPP with PHP 7

Download XAMPP from here: https://www.apachefriends.org/download.html
Make sure XAMPP is powered by newest version of PHP 7.
Install XAMPP.


STEP 3: Prepare Laravel Installation

Check server requirements:

  1. PHP version >= 5.5.9
  2. OpenSSL PHP Extension
  3. PDO PHP Extension
  4. Mbstring PHP Extension
  5. Tokenizer PHP Extension

You can use phpinfo() for that.


STEP 4: Install Laravel Coding Environment

Info:
Laravel coding environment is a place (framework environment), where you code your app.
There is more than one way to install Laravel coding environment for your App.
Let’s see them all – with some sample code.

way #1:  Install using Laravel Installer (via Composer)

First, we need Laravel installer downloaded – unless we have already done so before, e.g. for a different app.
Laravel seems to install its Installer to:

So, regardless where you are in your Command Prompt, Laravel installer will be found by Windows.
You may want to physically go to above directory to see, if it there.

Here is how to get Laravel installer:

  1. open your Command Prompt
  2. type in code:

Once installation is done, so are you … with this task.

Note:
You have to register Laravel installer path with your Windows “Path” environment variable.
Check here how it is done.

To do all installations, you are going to use Command Prompt, as you are installing local coding environment on Windows using XAMPP.

For anything done on server you most likely need something like Putty (instead of Command prompt).
On Linux, you’d need console … etc.
But this is out of scope of this “tootsie”, so I will not go into that.

So, let’s see different approaches leading to get you ready to code in Symphony’s offspring – Laravel.
Here is what to write into Command Prompt, to prepare Laravel coding environment.

option #1

If you registered Laravel installer path with “Path” environment variable (see several lines above), you should:

  1. go to directory, where you wish to install your project using Command prompt

    By typing above command to Command Prompt, you will switch to folder: my_projects.
    Inside of that project, you will install your Laravel coding environment.
    Of course, you can change folder: my_projects directory, to a real one you have on your server.

    If, your project directory is on different hdd, e.g. U: (as mine are), you need to switch disks before running code shown above:

  2. install your Laravel coding environment inside of directory selected above by using code like this

    Now, your Laravel coding environment is installed to: c:\my_projects\my_app

Note:
We are still not ready to go to browser and see initial welcome page.
But in a little while we will be able to.
Lets proceed and see other options, how to get Laravel coding environment in place using Laravel installer.

option #2

If, for whatever reason you do not want to, or you cannot register laravel installer path with “Path” environment variable, you can get your environment working without updating PATH enviroment variable for your Windows.

Here is how: go to directory, where you wish to install your project – see option #1 above how and install your Laravel coding envir. by using absolute path to your laravel installer.
It should look something like this:

Above code should install your Laravel coding environment into c:\my_projects\my_app

option #3

If, in addition to case described in option #2, you do not want to bother switching to directory, where you want to keep code for your app (which amounts to same place, where you install your Laravel coding environment) , you may use another absolute path to get the job done.

It is going to look like this:

Above code should have your Laravel coding environment (where you develop your app) installed using a single line of code in your Command Prompt.

So, if you are lazy (as any coder I have ever met), you may consider option #3.

If you register Laravel installer with Windows PATH envir. variable, you will save on typing those long absolute paths when launching future Laravel app environments.

Kind of: pay me now, or pay me later stuff.

way #2:  Via Composer Create-Project Command

If you wish to avoid all these tasks with installing Laravel installer and registering it with PATH, you can use Composer internal command: Create-Project to get your app coding environment ready.

option #1:

Use relative paths.

  1. first get inside of folder, which will hold your app folder using Command Prompt

    Of course, you can change folder: my_projects directory, to a real one you have on your server.
    If, your project directory is on different hdd, e.g. U: (as mine are), you need to switch disks before running code shown above.
  2. then use composer command in Command Prompt to get Laravel coding setup for your app.

    Now, your Laravel coding environment is installed to: u:\my_projects\my_app

option #2:

Use absolute paths.

You can combine option #1 code into one line, by using absolute path:

Now, your Laravel coding environment is installed to: c:\my_projects\my_app

 


NOTE:

If you want to install given version, use this code:

 

Laravel 5: Startup – Set Environment System Variable Path (windows) for Composer

How to make sure that Composer variable PATH was properly added (windows)

Interface way

Go to Desktop and right click on “This PC” link (it can be labeled My Computer or similar) and select Properties from context menu.

lar-1-thispc

 

This will get you Control panel subsection.
Once you are there – click on “Advanced System Settings”

lar-2-ctrl-panel

Using direct access
  1. click Windows ‘Start’ button on keyboard (it is usually to the left from Space button and had Windows logo on it)
  2. on the left-bottom write: ‘sysdm’ and hit ‘Enter’
  3. open tab: Advanced

 

Both ways described above will get you “System Properties” – click on “Environment Variables”.

lar--3-sys-props

 

In newly popped in “Environment Variables”, double-click on “Path”

lar-4-evars

 

This will bet you another manager, where you can edit Path.

lar-5-edit-path

 

Since, “Variable Value” field is rather small – you may want to consider pasting it to Notepad.
Check, where Laravel installer was installed – in my case it was in:

Add your Laravel installation folder link to Path value string.
Remember, that paths are divided by semicolon.

lar-6-composer

Important Notes:

You must reboot computer for new Path to be available

You can try relative Path:

instead of absolute path used in example above.: