Skip to content

Instantly share code, notes, and snippets.

View ziadoz's full-sized avatar

Jamie York ziadoz

View GitHub Profile
@ziadoz
ziadoz / web.php
Last active October 25, 2024 14:53
Using Laravel 11's contextual attributes to populate a data object from POST data
<?php
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Container\ContextualAttribute;
use Illuminate\Support\Facades\Route;
/**
* Create a new contextual attribute that grabs fields from the request POST data.
*
* The equivalent of doing `$request->post($key, $default)` in a controller.
@ziadoz
ziadoz / tables.sql
Last active October 24, 2024 14:23
Table Timestamp Column Ordering - Put timestamps after id instead of at the end of the table
-- Typically it's common to add timestamp columns to the end of a database table, like this:
create table links (
id bigserial primary key,
link text,
created_at timestamp without time zone not null default now(),
updated_at timestamp without time zone not null default now(),
deleted_at timestamp without time zone null default null
);
-- However, when new columns are added it starts to look weird
@ziadoz
ziadoz / coins.php
Last active November 5, 2024 20:50
Coins - Find minimum number of coins needed to hit target (denominations can be reused)
<?php
function mincoins(array $coins, int $target): int
{
// Check the simplest answers first...
if (count($coins) === 0) {
return 0;
}
if (in_array($target, $coins)) {
return 1;
@ziadoz
ziadoz / links.txt
Last active October 6, 2024 18:22
Postgres 17 Playground
@ziadoz
ziadoz / readme.txt
Last active September 26, 2024 15:46
PGLoader - Importing MySQL into Postgres
# Docker
docker run --rm -it --network="host" --add-host host.docker.internal:host-gateway dimitri/pgloader:latest pgloader --verbose --debug --client-min-messages debug --log-min-messages debug mysql://<user>:<password>@127.0.0.1:3306/dbname postgresql://<user>:<password>@127.0.0.1:5432/dbname
# Brew
# @see: https://github.com/dimitri/pgloader/issues/962
brew install pgloader
pgloader --verbose --debug --client-min-messages debug --log-min-messages debug --dynamic-space-size 262144 -v pgloader.load
# Config (pgloader.load)
LOAD DATABASE
@ziadoz
ziadoz / reset_macos_firewall.txt
Last active September 20, 2024 22:24
Reset macOS Sequoia Firewall
Disable the firewall:
/usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate off
Remove the firewall config (**doesn't work due to permissions**):
sudo rm /usr/libexec/ApplicationFirewall/com.apple.alf.plist
Or, backup the firewall config (**doesn't work due to permissions**):
sudo mv /usr/libexec/ApplicationFirewall/com.apple.alf.plist /usr/libexec/ApplicationFirewall/com.apple.alf.plist.bak
Enable the firewall:
@ziadoz
ziadoz / loops.js
Last active August 29, 2024 15:31
JS Loops Example (for...in, for...of, forEach(), entries(), keys(), values())
/**
* Array Looping
*/
const arr = ['foo'];
// A for...in loop will return the key...
// @see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
for (const key in arr) {
console.log(key);
}
@ziadoz
ziadoz / index.blade.php
Last active August 29, 2024 14:48
Laravel Blade Props
@php
$attr3 = 'baz';
$attr4 = 99_999;
$attr5 = new stdClass;
$attrWtf = 'wtf';
@endphp
<x-my-component
attr1="foo" {{-- Component receives string --}}
attr2="{{ 'bar' }}" {{-- Component receives string --}}
@ziadoz
ziadoz / config.php
Created August 21, 2024 11:17
Pydantic Settings for PHP
<?php
// @see: https://docs.pydantic.dev/latest/concepts/pydantic_settings/#usage
final readonly class DatabaseConfig
{
#[Concat(
#[Env('DB_HOST')],
#[Str('://')],
#[Env('DB_USER')],
#[Str('@')],
@ziadoz
ziadoz / MyCommand.php
Last active August 20, 2024 19:31
Laravel 11.21 Container Contextual Attributes
<?php
/*
Contextual attributes were implementated by @innocenzi and @ollieread:
- https://github.com/laravel/framework/pull/51934
- https://github.com/laravel/framework/pull/52428
I added contextual attributes for the core drivers:
- https://github.com/laravel/framework/pull/52265