Laravel 5: Setting-up Testing Environment – Code used in examples

Code used in examples explained in detail.

First, lets see at some code that we are going to use to generate our examples.

Note:
I do everything assuming you are on Windows localhost (e.g. XAMPP) – on other OS, e.g. online LAMP server, you may need to modify my instructions to some extent.

Lets install Laravel

What I use here is slightly modified Laravel “The Quickstart” example.

Let me explain how to install it, as it may be challenging to a beginner, since creators of documentation missed a step or two.

Here is original code picked of site to which link given above goes (with some modification):

So, lets look at what’s above step by step.

Above code is going to install on your computer (localhost), or server.
Lets assume it is localhost.
First decide into which folder you want to download this example files – lets say it would be c:\mysites.
So, in Command Prompt you type: cd c:\mysites.
“quickstart” is a directory inside of c:\mysites, where you find your intermediate example files downloaded.

Now we are going into our example directory, so we can run next command.

Also in the future, to access php artisan commands, tinker, or composer from command prompt, you have to get into that directory first. In our case it is quickstart, but your may be anything you choose it to be.
You may also read these links to get more info on the installation subject:

Now, lets look at our missing steps.

Step 1:
You need to set your database and get some credentials ready for Laravel.

Step 2:
Go to main directory and find file called .env
Inside look for this code and fill it with credentials from step 1.

Now you could run next line of code and finish with data migration to your database, but wait.
Let’s first add that missing column to tasks table, which we will need in our example.

Migrations and Seeding is described in detail here.

 


We are done installing – lets look at example coding.
I prefer to tinker with code directly – I will use a couple of Laravel files directly to apply example code.
Model located at:

Controller located at:

So, all code testing I use here is done in these 2 files.

 


LARAVEL 5.3

Laravel 5.3 does not have Tutorials (Basic, Intermediate) anymore.
Here is even better replacement.

In case that great tutorial (git repo of it)  ever disappeared, here is full zip of it: laravel5-3-example-master

Just follow installation instructions and you’ll be up in a few minutes.
Instructions are in Readme.md file.
Author made it extremely easy … even for beginner.

 


NOTE:

If you are getting this error:

Try running this code in Command Prompt:

Now run seeding command again:

Sometimes you may also run this code as well to get rid of that error:

… and seeding command.
But usually, all you need is to regenerate class autoloader file using optimize command.


Just one more piece of information – how to access it from browser.

  • navigate to your example address – be it one on localhost, or one on server
  • create a profile – register
  • login using profile
  • you are there

Now, we are ready with our example skeleton.