The pipeline pattern is one way to refactor your code to separate concerns. Here is an example of un-refactored code that uses individual if statements to process some data. I’ll keep the details out for clarity. This code seems simple
The current state of things
It’s amazing how life gets going in a rhythm, and you get your head down and working so hard, that you sometimes forget to look around (or in this case, write in your blog). Working at a small travel advertising
Sorting Associative Arrays in PHP
$arr = [ ‘foo’ => ‘bar’, ‘faz’ => ‘baz’ ]; array_multisort(array_column($arr, ‘value’), SORT_ASC, $arr); You can sort an associative array with one line of code as I did above. As a result, the array becomes sorted by the keys: [
WordPress permalinks not working
WordPress permalinks not working? I feel your pain. Read on… Recently, after setting up another virtual host on my Linux server, for a WordPress site, permalinks were not working. Any page other than home was getting a 404 error. I
Laravel – the difference between all() and get()
When dealing with a model, all() is a static method. It creates a new query object, and runs get() on that object get() is not static, but can be called statically because of a magic method in the Model class.
Laravel Eloquent only()
$fullModel = Model::find(1) // Get model with id 1 $idArray = $fullModel->only(‘id’) // array containing id // this does not work. You’ll get back an empty collection // It is trying to pull the id column off the collection object,
MySQL Stored Procedures
Here is a tip you can use when writing a stored procedure, and using prepared statements. Since you have to pass the statements in through EXECUTE stmt USING … it can get confusing if there are a large number of
Laravel Eloquent Attributes
When dealing with an Eloquent model, you can add your own attributes. These can be useful for computed values. For example: class Person extends Model { // By default, all the database fields will be available. // Let’s assume for
SQL EXPLAIN: The “Extra” column
This column contains additional information about how MySql resolved the query. There is a line in the manual which states: If you want to make your queries as fast as possible, look out for Extra values of Using filesort and Using temporary. Let’s examine why
SQL EXPLAIN: The “filtered” column
The filtered column shows an estimated percentage of rows that will be filtered by the table WHERE clause. It’s typically only shown during the EXPLAIN EXTENDED command.
SQL EXPLAIN: The “ref” column
The “ref” column shows which columns or constants are compared to the index named in the “key” column to select rows from the table. This comes right from the manual, but I have noticed that when the value is const,
SQL EXPLAIN: Index-related columns
These relate to the usage of indexes: possible_keys Which indexes were considered? key Which indexes did the optimizer choose? key_len How many bytes of the index will be used?
SQL EXPLAIN: The “type” column
The “type” column indicates how MySQL will access rows From worse to better: ALL index range ref eq_ref const system NULL
A faster alternative to correlated sub-queries
I recently had to rewrite a query that was running very slowly. It was taking over 30 seconds. Upon analyzing it, I saw that it had 2 correlated sub-queries in it, and each was doing a count(*). This meant that
You might not need jQuery…
I just found a video that talks about how jQuery is no longer needed in 2018. I found it interesting, and thought I’d save it and some related pages here for future reference. http://microjs.com/#ajax https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch https://caniuse.com http://youmightnotneedjquery.com/
Compact and Extract
Compact takes individual variables, and packs them into an associative array. Extract does the opposite. <?php // example code // Set 2 variables $var1 = “one”; $var2 = “two”; // Create associative array $arr = compact(‘var1′,’var2’); // Show results var_dump($arr);
Splat!
While looking through the Query Builder class in the Laravel source code today, I came across something I hadn’t seen before in PHP: $query->{$method}(…array_values($value)); I had no idea what the three dots meant, so I went looking for it. Turns
Understanding Octal
Why would we need to understand Octal? The most relevant reason I can come up with right now is that looking back at some of the interview question challenges I’ve posted, there is one that deals with an Octal number.
foreach vs array_filter
The problem: Given an array of numbers, create a new array containing only the odd numbers from the original. $original_array = [1,2,3,4,5,6,7,8,9,10]; There are two ways you can approach this problem. Let’s look at a foreach loop first: $original_array =
Factor Finder
// Number to factor $num = 60; // Loop from the number to factor down to 1 for($i=$num; $i>=1; $i–){ // If it divides evenly if($num % $i === 0 ){ // Output result of division, which is a factor