Skip to content

Instantly share code, notes, and snippets.

@kmuenkel
kmuenkel / Request.php
Last active November 4, 2021 16:53
Laravel Request Parameter Filtering: Remove request parameters not defined in the request ruleset, if there is one. Returns false if parameters were attempted, but none were permitted.
<?php
namespace App\Http\Requests;
class Request extends FormRequest
{
/**
* @return bool
*/
public function filterFields()
@kmuenkel
kmuenkel / helpers.php
Last active February 5, 2025 23:28
AES Encryption: Handle encryption in the same manor as MySQL's AES_ENCRYPT() and AES_DECRYPT() functions, using openssl instead of the deprecated mcrypt.
<?php
if (!function_exists('mysql_aes_key')) {
/**
* Produce a version of the AES key in the same manor as MySQL
*
* @param string $key
* @return string
* @see https://www.smashingmagazine.com/2012/05/replicating-mysql-aes-encryption-methods-with-php/
*/
@kmuenkel
kmuenkel / helpers.php
Last active November 4, 2021 16:51
SQL Debugging: Set a start and end point, and gather some data on all queries executed between those two points.
<?php
if (!function_exists('debug_query')) {
/**
* @param bool $trace
* @param bool $full
* @param bool $cache
* @return @void
*/
function debug_query($trace = true, $full = false)
@kmuenkel
kmuenkel / AppServiceProvider.php
Last active August 9, 2019 05:32
Laravel Parameter-Specific Custom Validation Messages
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Overrides\Illuminate\Validation\Factory as FactoryOverride;
/**
* Class AppServiceProvider
* @package App\Providers
@kmuenkel
kmuenkel / InstanceHandlerJob.php
Created August 9, 2019 05:31
Fan-out the handling of items in an array using asynchronous queue jobs.
<?php
namespace App\Jobs;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use App\Models\PayrollRequest;
use Illuminate\Support\Collection;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
<?php
/**
* @param array[] $matrix
* @param string[] $fields
* @return array[]
*/
public function tilt(array $matrix, array $fields = array())
{
$topKeys = array_keys($matrix);
$nestedKeys = $fields ?: array_keys((array)current($matrix));
@kmuenkel
kmuenkel / DebugTrace.php
Last active April 10, 2021 19:49
Get a stack trace at any point in your code in a format that's easier to consume
<?php
namespace App\Helpers;
use Exception;
use ReflectionMethod;
use ReflectionFunction;
use ReflectionException;
/**
@kmuenkel
kmuenkel / DbQuery.php
Last active September 22, 2021 04:14
Laravel Event that parses all queries and their execution stack traces. (Dependent on StackTrace.php)
<?php
namespace App\Helpers;
use PDO;
use DateTime;
use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\QueryException;
use Illuminate\Support\{Str, Arr, Carbon};
@kmuenkel
kmuenkel / LinkModels.php
Last active January 28, 2020 19:21
Link any two models according to the nature of the specified parent Model relation
<?php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
if (!function_exists('relate_models')) {
/**
* @param Model $model
* @param Model $relatedModel
@kmuenkel
kmuenkel / RegisterInDir.php
Created January 28, 2020 19:08
Laravel Helper function for loading all classes in a given directory
<?php
if (!function_exists('load')) {
/**
* Register all of the commands in the given directory.
*
* @param array|string $paths
* @param string $parentClassName
* @param callable|null $register
* @see Illuminate\Foundation\Console\Kernel::load()