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
EDH

New Vanity in EDH

We put in a new vanity last weekend at the EDH house, it turned out to be a fun little project. We snapped some pictures along the way which are below:

The Old Vanity
Old Vanity Removed
Tracing needed cuts so the top will fit flush
New Vanity Installed
New Mirror Installed

 

Categories
Finance Samuel

Favorite ERE Quotes

These are all copied from: http://earlyretirementextreme.com/frequently-asked-questions

Below is a list of my favorite tidbits of info from ERE.

In permaculture, one tries to understand how different plant and animal species interrelate with the goal of creating an arrangement with the highest possible production using the least amount of effort. The idea is to replace doing with thinking. Permaculture is brain-intensive rather than pesticide and fertilizer intensitive. You locate one plant (your job) close to a second beneficial plant (your home) which automatically fertilizes the first so you save on fertilizer (transport). You arrange plants (things you do) to minimize evaporation (wasting) so you no longer have to water them (spend). Eventually, your life will be so optimally designed and arranged in such a way so as to get paid for participating in your hobbies, get free food (from the garden) and exercise and eliminate unhealthy sideeffects like illness and stress.

Conversely, a non-permaculture way of thinking focus on increasing yield (income) of a single crop (your job) by increasing the amount of fertilizer (effort), using genetically modified seeds (technology and gadgets). You see the analogy, right? Actually a lot of the comments I get about ERE being “too extreme” and “too much sacrifice” is from those who do not see the analogy. You can imagine how an industrial farmer sees permaculture before understanding it: “Wow, your production (standard-of-living) must be low (austere) given the amount of fertilizer (money) you use (spend).

Q: How can someone with children retire early?
A: The same way as people without children. By themselves, children actually spend very little money. The problem is parents spending money on their children without limits. If you adopt the same basic guidelines for your children as you do for yourself, the cost will be low. The fiscal or frugal problem happens when parents are willing to spend less on themselves but still want to create a consumer lifestyle for their children, usually with the goal to conform. I believe this is doing the children a disfavor. Unlike stuff which you can just put in your garage, children need attention which you can either provide yourself or pay someone else to provide for you. Early retirement is a great way to provide time and attention and if you’re smart you will wait the 5 years it takes to save enough money to be financially independent before having children.

Q: I think 30 is way too young to be retired!
A: Could it be that you’re stuck in the conventional “school-career-retire-die” way of thinking about life? If so, you need to read a bit more of this site because that’s NOT the kind of retirement ERE is about. Here retirement is used in the “becoming financially independent and using that freedom to pursue other interests”-sense. Incidentally, this is not a new idea. Rather it is an old and somewhat forgotten idea. If you read biographies of people like Ben Franklin or Joseph Conrad, you will often see that they “retired” from one profession to take up another interest. Being financially independent and also well-rounded and possessing more than one skill made that possible.

Q: Do you have a bucket list?
A: Not officially! My goal in life is simply to avoid boredom. However, here are some things that interest me and a few things I have done: Get a PhD. Publish a scientific paper. Become financially independent. Live in a second country. Live in a third country. Live in a fourth country? Visit more than 10 other countries. Become a millionaire. Live in an RV. Live on a boat. Work on Wall Street. Work on/in the space program. Work in a strategic role (security or politics, not business). Learn to sail. Get a HAM radio license. Build a radio. Get my own wiki page. Publish a bestseller. Be published in three separate fields. Build a house. Buy a house. Sell a house. Fix a car. Build a vehicle. Take a trip walking 1000+ miles. Take a trip cycling 2000+ miles. Take a trip sailing 4000+ miles. Climb a mountain (Mt. Fuji). Fly an airplane. Get a black belt in a martial arts. Develop enough skills to live well on less than $7500/year. Less than $5000/year. Less than $2500/year. Become completely self-reliant: $0/year. Live off freelance income. Live off investment income. Live off business income. Live off wage income. Patent an invention. Learn woodworking. Learn metalworking. Ride a motorcycle. Shoot a gun. Build a steam engine. Build an entire machine toolshop (Gingery style). Go to Alaska and build a cabin to live in Proenneke-style. Build a robot. Build a boat. Build a motorcycle. Live forever…

Categories
Samuel Voluntary Discomfort

First Voluntary Discomfort Session of 2018

As a general practice, I have really started to find the act of Voluntary Discomfort to be very enjoyable and worthwhile. This practice is something that I have done for a long time but I have never been intentional about it. I’ve only occasionally done things like taking walks in freezing weather or taking cold showers to start or end a day. Every time I had no plan though, I simply started and stopped at a random interval. Moving forward I’m going to be intentional about this practice from now on, I’m planning to do each act of Voluntary Discomfort in a 30 day series and also journal the results.

First up for the month of January will be the Voluntary Discomfort of spending no money for the entire month (or as long as we can go). To start this challenge we did the following:

-Filled up our cars with gas on 12/31/2017
-Purchased groceries on 12/31/2017

And that’s pretty much it! We are simply going to try and see how long we can go without making a single purchase. Luckily we both do not have very far commutes so we think the tank of gas in each vehicle should come pretty close to lasting the entire month. Hopefully, we can last the entire month, that does seem pretty extreme though so we will see how far we can get!

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

Favorite MMM’s

This is just a running list of my favorite MMM posts as I re-read his entire blog:

It’s All About the Safety Margin

Springy Debt instead of a Cash Cushion

The 4% Rule: The Easy Answer to “How Much Do I Need for Retirement?”

The Shockingly Simple Math Behind Early Retirement

Killing your $1000 Grocery Bill

**https://www.mrmoneymustache.com/2011/10/25/the-joy-of-part-time-work/

—not MMM’s but other good articles—

Frequently Asked Questions