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
Fixing Stuff Samuel Technology

Fixing Nest Thermostate In 20 Degree Weather

We woke up today with a nest thermostat was showing “dead battery”. After some googling around we realized we could take it off the wall and plug it into a USB cable to charge it. So we did that and 10 minutes later it was showing the message “connect to base”. So we put it back on and still no luck, it showed wiring error E74. After some more searching around we came across a nest support page that explained when the weather gets cold there is sometimes a safty shut off switch on your AC that activates preventing the nest from getting power. The solution is to simply unplug the Y1 power and plug the nest back into the base. The Unit should instantly kick on and start heating again:

Link to nest support page documenting how to remove the Y1 wire: https://nest.com/support/article/Troubleshooting-Nest-Thermostat-power-errors-when-it-gets-cold-outside

If you keep running into this issue, you can change the ground wire into a commmon wire buy following the instructions on this video: https://www.youtube.com/watch?v=R039DH7HASg

Categories
Fixing Stuff Samuel

Bulk Archive Emails in Gmail inbox

Thunderbird was really starting to run slowly with the weight of thousands of emails in my inbox. So I went ahead and did a little research on how to bulk archive emails in gmail. What I wanted to do was archive any emails older than 3 months old, it turns out there is a very easy way to do this in gmail:

Step 1: Enter this in the top search bar on the gmail desktop browser

before:yyyy/mm/dd

Step 2: update the above code with the actual date you would like to filter by

Step 3: Select all the emails on the page, then select “apply to all” and archive.

That’s it, the page should process once you hit the small archive button and then all messages should move into the “all mail > archive” folder.

Categories
Fixing Stuff Samuel

Show Unread Emails In Primary Gmail Inbox

There was an annoying issue happening in my gmail inbox today, I had 4 unread messages but 3 of them ended up being very old and I could not find them easily. When I tried to find the unread emails in my gmail primary inbox I tried typing “filter: unread” in the top search box but that search included all of the unread emails in the promotions tab and the social tab which made it impossible to find the truly unread messages in my primary gmail inbox. To find just the primary inbox messages you can copy the code below into the search box at the top of the gmail page:

label:unread in:inbox -category:social -category:promotions -category:updates -category:forums

Once you copy that code above into the top search box in your gmail account, it will run and find only the unread messages in your primary gmail inbox. From there you can mark the individual messages as read or delete them, and if you are anything like me then your mail notifications icon will go away on your phone and you can get back to your regular life without being nagged by the little unread email icon every time you open your phone 🙂

Categories
Fixing Stuff Samuel

Fixing Internet Connection when connected to VPN

We were having an issue with our VPN setup and just wanted to document the fix. When working remotely we preferred to VPN back to the office from time to time but we were running into an issue where when connected to the office VPN we could not browse the internet. Everything else worked fine, our file share on the office network, we could send/receive email, print at the office ect… the only thing that was not working was actual internet browsing. To fix the issue we found a great piece of helpful information on Stack Overflow. The info is copied below for easy access and there is also a link to the stack overflow thread:

Link to thread: Here

Working steps to take:

uncheck “Use default gateway on remote network” in the TCP/IPv4 advanced settings for the VPN connection.

nbiwl

Categories
Fixing Stuff Laravel Samuel

Fixing Cron Job Error on Laravel 5.1 application

Background: We updated to php from 5.2 to 5.6 and our cron job starting firing an error. The steps taken to fix this are:

1.) Verify that argc_argv is set to =”On” in the default server php.ini file.

2.) Modify the cron job command:

original: php /home/username/path-to-laravel/artisan schedule:run >> /dev/null 2>&1

updated: php -d register_argc_argv=On /home/username/path-to-laravel/artisan schedule:run >> /dev/null 2>&1

——Update 11/30/2016——

We had issues with this cron again and the way to fix things was the following:

1.) login to WHM and navigate to MultiphpINI Editor > Editor Mode
2.) Select the current system default version of PHP (if not sure navigate to the Multiphp Manager page and it displays the system default at the top)
3.) On Editor Mode page navigate to Line 654 and set register_argc_argv = On

Save the file and your done.

Useful Testing info:

In this process I needed to figure out how to run an individual command from the laravel scheduler…. since the scheduler was set to only run the command I needed once per day I just wanted to manually run that command. To Do this we open up the actual command file:

app > console > commands and then find the command file you want. Then open the file up and find the “protected name”

command to run on the server:
php artisan 'protected name'. *note include the quotes when you copy this into your SSH terminal.