Skip to content

Mastering PHP and Laravel

Never stop learning!

Menu

  • Home
  • Blog Posts

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

Kenny October 9, 2018January 21, 2023 Blog Posts, Laravel, Learning, PHP, Programming No Comments Read more

Laravel Eloquent sortBy function

Here is how you can sort a collection by an ordered array of ids. The use case here was that I had a number of objects in a Redis cache, which were retrieved in unsorted order. The objects were large,

Kenny October 9, 2018January 21, 2023 Blog Posts, Laravel, PHP, Programming No Comments Read more

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

Kenny September 25, 2018January 21, 2023 Blog Posts, Learning, MySQL, Programming No Comments Read more

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.

Kenny September 25, 2018January 21, 2023 Blog Posts, Learning, Programming, SQL No Comments Read more

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,

Kenny September 25, 2018January 21, 2023 Blog Posts, Learning, Programming, SQL No Comments Read more

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?

Kenny September 25, 2018January 21, 2023 Blog Posts, Learning, Programming, SQL No Comments Read more

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

Kenny September 25, 2018January 21, 2023 Blog Posts, Learning, Programming, SQL No Comments Read more

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

Kenny September 19, 2018January 21, 2023 Blog Posts, Learning, Programming, SQL No Comments Read more

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/

Kenny September 5, 2018January 21, 2023 Blog Posts, Javascript, Learning, Programming No Comments Read more

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);

Kenny August 15, 2018January 21, 2023 Blog Posts, Learning, PHP No Comments Read more

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

Kenny August 15, 2018January 21, 2023 Blog Posts, Learning, PHP No Comments Read more

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.

Kenny July 26, 2018January 21, 2023 Blog Posts, Learning, PHP, Programming No Comments Read more

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 =

Kenny July 23, 2018January 21, 2023 Blog Posts, Learning, PHP No Comments Read more

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

Kenny July 18, 2018January 21, 2023 Blog Posts, Learning, PHP, Programming No Comments Read more

chmod

chmod is short for change mode. It is the linux command used to change file permissions. The basic format of the command is like this: chmod options permissions filename There are a number of options, but one of the more

Kenny July 17, 2018January 21, 2023 Blog Posts, Learning, Linux No Comments Read more

Changing the color of the first word

The question/problem is: Write a PHP script, which changes the color of the first character of a word. This challenge tests your knowledge of preg_replace, and regular expressions. $text = ‘PHP Tutorial’; $text = preg_replace(‘/(\b[a-z])/i’,'<span style=”color: red;”>\1′,$text); echo $text; ?>

Kenny July 16, 2018January 21, 2023 Blog Posts, Learning, PHP, Programming No Comments Read more

SQL sub query challenge

Goal: Write a query which will get the first and last name from the customer table, and the total amount of all that customer’s orders. SELECT first,last FROM customers C This will get all the customers.Next we need to write

Kenny July 15, 2018January 21, 2023 Blog Posts, Learning, Programming, SQL No Comments Read more

Reference challenge

This one tests your knowledge of how reference variables work: $a = ‘P’; $b = &$a; $b = “PH$b”; What will $a and $b be after these three lines of code run? First, $a is set to ‘P’. Then, $b

Kenny July 12, 2018January 21, 2023 Blog Posts, Learning, PHP, Programming No Comments Read more

Base class or Interface?

When making the decision to use a base class or interface, it may help to think of the implementation. If all the implementations are going to be similar, then you might want to group them under a base class. But

Kenny July 11, 2018January 21, 2023 Blog Posts, Learning, Programming No Comments Read more

preg_match problem

Given: $a = “4|6|3|5|x|2|”; Write code that will output: “2|3|4|5|6” It could be done the long way round, by turning the string into an array, looping over each element, testing each item to see if it’s a number, and placing

Kenny July 10, 2018January 21, 2023 Blog Posts, Learning, PHP, Programming No Comments Read more
  • « Previous
  • Next »

Archives

Categories

  • Blog Posts (90)
  • iPhone (2)
  • Learning (42)
  • Linux (4)
    • Debian (1)
  • Mac Terminal / Linux (6)
  • Other Stuff (1)
  • Programming (68)
    • .NET (1)
      • C# (1)
    • Java (1)
    • Javascript (6)
      • AngularJS (1)
      • Node.js (2)
    • MySQL (3)
    • Objective C (1)
    • PHP (45)
      • Community (5)
      • Laravel (9)
        • Eloquent (3)
    • SQL (7)
    • Tools (2)
      • PHP Storm (1)
  • Slim Framework (1)
  • Uncategorized (9)
  • WordPress (3)
Copyright © 2023 Mastering PHP and Laravel. All rights reserved. Theme Spacious by ThemeGrill. Powered by: WordPress.