Categories
Fixing Stuff Laravel Samuel

Class PDO Not Found Laravel 5.6

When trying to deploy a commit on a Laravel 5.6 install we ran into the following error:

Class PDO not Found

In our particular case this is on a brand new site that was recently setup. This is only the second commit for the whole project. To fix this issue we ran through the following steps:

1.) Update composer (to do so we needed to to allow f_open)

2.) Delete the current vendor directory (please note this current vendor directory had been previously installed with f_open disabled and it appears to have caused some vendor files to load incorrectly)

3.) Run Composer Install

4.) Enable PDO (https://github.com/snipe/snipe-it/issues/1210)

5.) Run php artisan migrate

6.) Run php artisan db:seed

7.) Fire up our APP and enjoy no errors 🙂

Categories
Fixing Stuff Laravel Samuel

Updating Composer

We ran into an issue today trying to run composer update. The following error message was displayed:

[Composer\Downloader\TransportException]
The "https://packagist.org/packages.json" file could not be downloaded: allow_url_fopen must be enabled in php.ini (https:// wrapper is disabled in the server configuration by allow_url_fopen=0
failed to open stream: no suitable wrapper could be found)

TO fix this we completed the following steps:

1.) Opening WHM > MultiPHP INI Editor

2.) Select the version of PHP that is active for our specific account (you can look at MultiPHP manager to find your active version of PHP for each account)

3.) Find allow_url_fopen and set it to enabled.

4.) Run composer update again, once it completes turn allow_url_fopen back to disabled as its a security risk to leave this enabled long term.

Categories
Fixing Stuff Laravel Samuel

Updating Composer To Use New Version Of PHP

While installing a new Laravl project on my local machine I ran into the need to update the PHP version. I’m on a Windows 7 machine so there are two places that I needed to update the path variable. To do this follow these steps:

1.) Hit start and type “environment variable”. Click “edit environment variables for your account”. This will bring up a window that looks like this:

2.) Select Path and Click Edit in top section (see screenshot)

3.) In the text input field find the snippet that lists the path to PHP it will look like this:

C:\Users\computer-name:\wamp64\bin\php\php7.0.10;

4.) Update the path to the new version of PHP you would like to use, then click ok. Note: you can find the exact file path on your machine by opening up the wamp folder and navigating to bin\php

5.) In the system variables section scroll down till you find path and click edit (see screenshot)

6.) Edit the path variable exactly the same way that we did in step 4

Notes:

– Once you complete these steps to test if you are now using the newer version of php you can click start > type cmd > then type: php -v. If you do not see the new version of PHP restart your computer and test again.

Categories
Fixing Stuff Laravel

Laravel -Searching Multiple Database Tables

This week we had a request come in from a client that involved creating a new database column and then performing a search across multiple columns to collect and display the data to end users of the system. While completing this task we learned the following great information:

1.) DD in the new google chrome. In the latest version of google chrome we were not able to dump and die from our controller. To get around this we added the following command to our controller:


#When using dd() in an ajax call, chrome needs a response code set
http_response_code(500);
dd($results);

The code above was added in place of the normal return results that sends results data from our DB query to the view. In total for testing purposes our public function now looks like:


public function datatables()
// do stuff


#When using dd() in an ajax call, chrome needs a response code set
http_response_code(500);
dd($results);
#return $results;

Now that our controller will spit out information that we can use to test/troubleshoot we will then fire up our local environment, navigate to the view for the page we are working on and then do the following:

1.) Hit F12 to pull up the chrome developer tools.
2.) Click on Network > XHR > Preview. This should take us to a page that looks like:

Chrome Developer Tools XHR Results Preview
Chrome Developer Tools XHR Results Preview

3.) Now when we update code in our controller we can save the file then go over to chrome dev tools right click on the results link and reload to see the result of our updated code. Example screenshot is below:

XHR-Refresh-Results

2.) Now we are onto the actual query that allows us to search through multiple DB tables and organize the results. I’ve copied the query below and bolded the two new lines that allow us to get the results the client wanted.


$datatable->set_query(function($search_sql, $order_sql, $having_sql, $limit_sql, $values) use ($form_id) {

$values['form_id'] = $form_id;

$results = DB::select("
SELECT SQL_CALC_FOUND_ROWS
u.id AS user_id,
CONCAT(u.first_name, ' ', u.last_name) AS name,
ec.cell_phone,
ec.home_phone,
shift_code,
DATE_FORMAT(u.employment_began_at, '%m/%d/%Y') AS start_date,
SUM(IF(YEAR(date_in) = YEAR(CURDATE()), total_hours, 0)) AS ytd_hours,
SUM(total_hours + historical_overtime) AS total_hours

FROM

form_submissions fs
JOIN forms f ON fs.form_id = f.id
LEFT JOIN users u ON fs.user_id = u.id
JOIN fs_overtime_documentation a ON a.form_submission_id = fs.id
LEFT JOIN forms ec_form ON ec_form.slug = 'emergency_contact'
LEFT JOIN form_submissions ec_form_submission ON ec_form_submission.form_id = ec_form.id AND ec_form_submission.user_id = u.id
LEFT JOIN fs_emergency_contact ec ON ec.form_submission_id = ec_form_submission.id
WHERE
u.active = 1 AND
f.id = :form_id AND
fs.status != 'denied'
{$search_sql}
GROUP BY u.id
HAVING 1=1 {$having_sql}
{$order_sql}
{$limit_sql}
", $values);
return $results;

Expanding on on the explanation above the first line we added is the easier of the two, it takes the Sum of the total hours column if the year = our current year:

SUM(IF(YEAR(date_in) = YEAR(CURDATE()), total_hours, 0)) AS ytd_hours,

The next thing we had to do was total up hours from two different database tables. We do this with:

SUM(total_hours + historical_overtime) AS total_hours

Its important to note the only reason this works is because of the LEFT JOIN a few lines below. This join takes total_hours and historical_overtime and adds them together if the particular user has hours in their historical_overtime DB column:

LEFT JOIN users u ON fs.user_id = u.id

Categories
Laravel Mac Samuel

Setting Up Local Environment For Laravel Dev On Mac

There has been quite a bit of change lately, and one of the biggest changes relates to my work and the computer system that work is being done one. I recently made the transition to a new office that has the policy of all work being done on a Mac. Which so far has actually been smoother than I thought, and I’ve started to enjoy the Mac. The first thing I did in this transition to Mac (well maybe not the very first thing, but close to it) was to get a local dev environment setup on the new Mac that would allow me to work with Laravel. I wrote down detailed notes in case anyone out there is struggling with the same setup you are in luck, just follow the steps below:

1.) Install MAMP. This one is pretty straight forward. Simply go to the MAMP website and download your version then follow the installer.

2.) Now that you have MAMP installed the first step is navigating to the htdocs directory and install Composer. This proved to be a funny challenge for me. The easiest way I’ve found (so far) to get to htdocs and install composer is:

-Open Up Mac terminal by going to the search icon in the top right of the Mac and type in Terminal, then follow these commands:

cd /

This will take us to the actual root directory. Now type:

ls

we should see the full list of folders, including application folder, now type:

cd /Applications/MAMP/htdocs

We should now be in htdocs folder.

3.) Next lets install composer, to do this go to composer website (https://getcomposer.org/download/)and then copy the following code into your terminal:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php -r "if (hash_file('SHA384', 'composer-setup.php') === '55d6ead61b29c7bdee5cccfb50076874187bd9f21f65d8991d46ec5cc90518f447387fb9f76ebae1fbbacf329e583e30') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" php composer-setup.php php -r "unlink('composer-setup.php');"

Assuming composer installs correctly, lets now move it into global directory:

mv composer.phar /usr/local/bin/composer

Now any projects in the MAMP htdocs folder should be able to utilize composer. Test this buy running:

Composer help

4.) Ok now lets install our first Laravel project. Our first step on this is to try and run:

Laravel new folder-name (replacing folder name with our project name)

At this point we should most likely get an error message of “command not found”. This is fine, lets circle back and run the following command in termianl:

global require "laravel/installer"

This should load laravel dependencies, and we should see a list of them as they load in. Now we can try to run our laravel command again:

Laravel new folder-name

Again we should have the same error message pop up “command not found”. This is due to us needing to add our environment variables so the laravel installer commands can be executed. So lets run:

echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bash_profile

This should add our path variable to our profile. To double check this lets run:

Vim ~/.bash_profile

The environment variable we just added in the previous step should be there: export PATH=”$PATH:$HOME/.composer/vendor/bin”

Now lets close the document:

Hit ESC, Type :wq, and hit Enter

Now we are ready to circle back and run our laravel install again:

Laravel new folder-name

Hopefully you get the success message this time ☺. If you are like me and Laravel installed but you got a message about needing to update PHP versions then hit the Link below to go to the next page that lists the steps to update the version of PHP composer is using. By default it appears composer uses the system version of PHP not the version that ships with MAMP.

Categories
Laravel Samuel Test Driven Laravel Series

Setting up phpstorm to run unit tests

In This post we will go through the process of setting up phpstorm to run our tests.

Step 1:

Our first step is to get to the phpunit settings portion of phpstorm, to do this we can type control + shift + a or we can type “shift shift” and then type phpunit. The easier method is to hit control + shift + a, type phpunit and select the first option with the “settings” next to it.

Step 2:

Once the settings screen pulls up we should already be at the phpunit section. If not just type phpunit in the search bar at the top left. On this screen you will need to input 2 variables to get your tests working:

C:\wamp64\www\project-name\vendor\autoload.php

C:\wamp64\www\project-name\phpunit.xml

The final result should look like the snapshot below:

phpstorm-tests

Step 3:

Now we are ready to run our tests. To do this you can hit shift + f10 or you can go to the top nav bar and select “run > run all”.

Other helpful notes:

-control f5 will run the most recent test again
-alt + shift + f10 will run all the tests. This is helpful if you have only been running one specific test and would like to go back to running all tests