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
Gitflow Samuel

Gitflow Real World Example

The notes below are from a real world example of updating WordPress core and plugins on a website stored in a repo following the gitflow model. We already went over a test run of updating WordPress in a gitflow repo in our earlier post, if you are new to working with gitflow you should start there.

Clone down a copy of the repo into htdocs

cd /
ls
cd /Applications/MAMP/htdocs
cd /project-folder-name
Git clone git@github.com:user-name/repo-name.git
Now fetch the headers:
Git fetch
Git remote (to see remote branches)

git lfs install (follow steps here if this is the first time doing this, we will need to install this on our system:
https://help.github.com/articles/installing-git-large-file-storage/)

npm install (check for packages.json first, this command will install all the packages listed in in package.json file)

Troubleshoot NPM Install

You will probably get an NPM install message of:
Darwin 14.5.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v6.11.2
npm ERR! npm v3.10.10
npm ERR! code ELIFECYCLE

npm ERR! libxmljs@0.14.3 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the libxmljs@0.14.3 install script 'node-gyp rebuild'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the libxmljs package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node-gyp rebuild
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs libxmljs
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls libxmljs
npm ERR! There is likely additional logging output above.

To fix this were going to run:
Sudo npm cache clean –f
Sudo npm install –g n
Sudo n 4.7.0
Sudo npm install npm –g
Sudo npm uninstall node-gyp –g

Npm install
*Now all packages should install correctly

Source: https://stackoverflow.com/questions/38058386/why-is-node-gyp-rebuild-failing-on-mac-osx-el-capitan/38102743#38102743

Open repo up in source tree and verify that master and dev are in sync

Open SourceTree and create repo from local
Checkout dev branch by clicking odouble-clickinguble clicking dev, or manually run:
Git checkout dev in terminal window
Review the SourceTree graph to confirm branches are in sync

To login to Plesk go to:
https://[[ipaddress]]:8443/
click database > phpmy admin export

Create local copies of the repo and DB and store separately in case of an error/issue
Copy the repo and sql dump file onto desktop

Create the local Database & activate the site to use it

Open gitignore to confirm file name of local DB config file.
Create new config file, usually wp-config.local.php. Copy a standard wp-config file and re-save it as wp-config.local.php

Start MAMP then open up phpmy admin
Create DB, import sql dump file from live, then add information to wp-config.local

Change website URL in wp_options table to use local url in both places (http://localhost:8888/site-name)

Troubleshoot 500 server error

Navigate to localahost:8888/folder-name
If we get this error:
Localhost is currently unable to handle this request. HTTP ERROR 500
Then look in the error log by opening up terminal and typing:
tail -n 20 /Applications/MAMP/logs/php_error.log

It shows us an error of:
[20-Dec-2017 18:37:18 Europe/Berlin] PHP Fatal error: Uncaught Error: Call to undefined function wp() in /Applications/MAMP/htdocs/churchill-mainsite/Churchill-Center-School/wordpress/wp-blog-header.php:16
Stack trace:
#0 /Applications/MAMP/htdocs/churchill-mainsite/Churchill-Center-School/index.php(17): require()

To fix this we need to create a wp-config.local (copy a full WP-config file). The previous one we copied was not a complete file and it was giving us the dreaded white screen of death!

Create New Feature Branch
git checkout -b feature/wp-update dev

Turn Permalinks off to test pages locally:
Go to settings > permalinks and set them to plain (it was on custom, /blog/%postname%/)

Update WordPress
-update core first
-check the plugins page to verify if any plugins are doing major releases (jumping a whole feature number)
-update plugins one by one checking site for errors after each update

Turn Debug on to verify no errors present:
In wp-config.local add define(‘WP_DEBUG’, true);

Commit code to feature branch:
Review code in Sourcetree and confirm which files we would like committed, then commit it to feature branch inside SourceTree.

Merge code to dev branch (submit site for QA testing on Dev)

git checkout dev
git merge feature/wp-update
git push (open source tree and only push dev branch)

**on a release branch we will use:
git merge --no-ff feature/wp-update -m “merge wp-update branch into dev”
git push (open source tree and only push dev branch)

*Submit site for QA then stop here waiting for QA approval*

If QA testing passes, then delete feature branch:
git branch –d feature/wp-update
git push --delete origin feature/wp-update (only if we pushed feature to the repo)

Create new Release Branch from dev branch:
git checkout dev
git checkout -b release/X.X.X (check repo version numbers)

Update readme file in release branch:
# Change version number in package.json and README.md files to line up with new #

Merge from release branch into Master:

*Download a copy of the live site files & DB before proceeding to next step*
git checkout master
git merge --no-ff release/X.X.X -m “merge wp-update branch into development”
git push

Tag merge into Master:

--follow semantic versioning numbers http://semver.org/. IE from 1.0.1 > 1.0.2
git tag -a 1.0.0 –m “updating from wordpress 4.8.1 to 4.8.2”
git push --tags
helpful links on git tagging:
https://git-scm.com/book/en/v2/Git-Basics-Tagging
https://gist.github.com/justinfrench/89712

Merge Release Branch into Dev:
git checkout dev
git merge --no-ff release/X.X.X -m “merge wp-update branch into development”
git push

Delete Release Branch:

git branch –d release-X.X.X
git push –delete origin release-X.X.X (only if this branch has been pushed to the repo)

Categories
Gitflow Mac Samuel

Updating WordPress Inside A Gitflow Repo

This document is comprised of testing notes while going through the learning process of updating a wordpress site in a repo organized under the gitflow model. The steps below can be followed to update a wordpress site. This document assumes you have MAMP installed locally on your machine.

Generic Useful Commands:
Git branch (shows current branch you are on)
Git branch –a (to list out branches in the repo)
Git status (to see info on our current branch)
Git remote (to see remote branches)
Git tag (to see list of tags)
Git add . (to stage files for a commit)_

Steps Taken to Setup first Gitflow Repo & Update WordPress:

Open terminal and navigate to htdocs:

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.

Clone the repo

To clone the repo we need to setup git access. Create the keypair following the directions here:
https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/

Then copy they key into github following the directions here:
https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/

Now Clone the repo down
Navigate to the folder you would like the repo stored in (you should already be in the htdocs folder from steps 1 and 2 above).

Git clone git@github.com:user-name/git-flow-test.git

Now fetch the headers:

Git fetch
Git remote (to see remote branches)

Define who we are in the Repo:
–please note this sets the user for all projects so you will only need to do this once

git config –global user.email your-email-address@your-domain

Create Master Branch:

git checkout -b master
git status (to see if we are on master)

Create Readme.md file
Create readme.md file and put into the repo:
touch readme.md
echo “## This is a markdown readme file” >> readme.md

Complete first commit and push:

Commit this file to the repo
Git add .
Git commit –m “adding readme file”
Git push

–please note if this is your first commit you can select to use the new git 2.0 simple commit type
git config –-global push.default simple

Branch from Master to create Development branch:

git checkout -b dev master

Add old version of wordpress to development:
–please note this is only for testing purposes. In a real life situation the old version of wordpress would already be on the dev branch and we would skip to the step below and create our feature branch from dev

Download old version of wordpress from here: https://wordpress.org/download/release-archive/
Physically move wordpress files into our project folder, then run

git add .
git commit –m “adding wordpress 4.8.1”
git push

— please note on first push we will need to run:
git push –set-upstream origin dev

Branch from development to create new feature branch:

git checkout -b feature/wp-update dev
--Please note feature branches should be named "feature/wp-update" or "feature/some-other-update"

Commit Code To Feature Branch:
–Please note we need to ensure that the local environment is running the same version of php that the client’s live site is running for testing purposes. Update this version in MAMP to mirror the client’s hosting environment

Run the WP install to setup the site:
–use root for local wp site username and root for local password

Run the WP update in WP admin, then run

git add .
git commit –m “updating from wordpress 4.8.1 to 4.8.2”
git push

Merge wp-update feature branch into development branch

git checkout dev
git merge --no-ff feature/wp-update -m “merge wp-update branch into dev”
git push

Branch from dev to create Release branch

git checkout -b release dev

Delete feature Branch

git branch –d feature/wp-update
git push --delete origin feature/wp-update

Merge from release branch into Master
–please note merge from release to master requires final approval from senior dev staff & final client approval

git checkout master
git merge --no-ff release -m “merge wp-update branch into development”
git push

Tag first merge into Master with 1.0.0
–follow semantic versioning numbers http://semver.org/. In this tutorial the first release is tagged with 1.0.0. In a real life situation please note the current version and update accordingly, IIE from 1.0.1 > 1.0.2

git tag -a 1.0.0 –m “updating from wordpress 4.8.1 to 4.8.2”
git push --tags

Helpful links on git tagging:
https://git-scm.com/book/en/v2/Git-Basics-Tagging
https://gist.github.com/justinfrench/89712

General Notes:
WordPress updates do not require database updates to be performed locally. Updating the files on the live server and visiting a few admin pages will perform the database updates on the live server. We should never make updates to a database locally and move that database live. The best possible workflow is one where code moves up and content moves down: https://pantheon.io/docs/pantheon-workflow/

When updating versions of PHP our workflow is:
-clone down site repo to local machine
-update version of php locally
-test site locally to make sure everything works
(if something breaks, create feature branch from dev & make fixes, test locally then merge to development).
-update version of php on development server
-final QA by PM
-merge code to release branch for final approval by senior dev
-update version of PHP on live
-merge code to master from release branch

When updating wordpress versions our workflow is:
-clone down site repo to local machine
-create feature branch from dev and update locally
-test locally to make sure everything works (use php version from clients live site)
-merge to development from release branch
-final QA by PM
-Merge to release branch for final approval by senior dev
-Merge to live

Random Notes:

Writing a commit message without using -m tag:

Press i to enter insert mode.
Now you can type your message, as if you were in a normal (non-modal) text editor.
Press esc to go back to command mode.
Then type :w followed by enter to save.
Finally :q followed by enter to quit.

Deleting Git Tags:

# delete local tag ‘12345’
git tag -d 12345

# delete remote tag ‘12345’
git push –delete origin tagName