Skip to content

Instantly share code, notes, and snippets.

View mikehins's full-sized avatar

Mike Hins mikehins

  • Trinary
  • Mont-Tremblant
  • 10:00 (UTC -04:00)
View GitHub Profile
@mikehins
mikehins / collections.php
Created February 13, 2025 10:22
Laravel collection helper
<?php
use Illuminate\Support\Collection;
$users = collect([
(object) ['name' => 'Alice', 'age' => 25, 'votes' => 5, 'active' => true],
(object) ['name' => 'Bob', 'age' => 30, 'votes' => 10, 'active' => false],
(object) ['name' => 'Charlie', 'age' => 35, 'votes' => 15, 'active' => true],
]);
/*@import url('https://fonts.googleapis.com/css2?family=Fira+Code:[email protected]&family=PT+Sans:ital,wght@0,400;0,700;1,400;1,700&display=swap');*/
@import url('https://fonts.googleapis.com/css2?family=Geist+Mono:[email protected]&family=Geist:ital,wght@0,400;0,700;1,400;1,700&display=swap');
:root {
--phpunit-breadcrumbs: #4b4b4b; /* Light grey for breadcrumbs */
--phpunit-success-bar: #2d6a4f; /* Dark green for success bar */
--phpunit-success-high: #6abf8f; /* Muted green for success high */
--phpunit-success-medium: #8cd9a3; /* Lighter green for success medium */
--phpunit-success-low: #d1f0d0; /* Very light green for success low */
--phpunit-warning: #4e4e2f; /* Dark yellow for warning */
Schema::table('users', function (Blueprint $table) {
$table->string('email_sanitized')->virtualAs('
CONCAT(
SUBSTRING_INDEX(email, "@", 1),
"@",
SUBSTRING_INDEX(email, "@", -1)
)
')->unique();
});
First Row of Many Similar Ones
SQL is a straightforward and expressive language, but it sometimes lacks constructs for writing queries in a simple way. It's more complicated than it should be to write a query to get, e.g., only the most expensive order for every customer of the current fiscal year. You can either use PostgreSQL's vendor-specific DISTINCT ON feature or window functions for every other database.
MySQL
SELECT *
FROM (
SELECT *, RANK() OVER(
PARTITION BY customer_id
# https://soft-builder.com/how-to-list-all-foreign-keys-in-mysql-database/
SELECT RefCons.constraint_schema, RefCons.table_name, RefCons.referenced_table_name, RefCons.constraint_name, KeyCol.column_name
FROM information_schema.referential_constraints RefCons
JOIN information_schema.key_column_usage KeyCol ON RefCons.constraint_schema = KeyCol.table_schema
AND RefCons.table_name = KeyCol.table_name
AND RefCons.constraint_name = KeyCol.constraint_name
WHERE RefCons.constraint_schema = 'DATABASE_NAME';
@mikehins
mikehins / rev.php
Created December 15, 2023 16:04
Add application version to Laravel app
// AppServiceProvider
public function boot(): void
{
//...
Blade::directive('rev', function () {
$v = config('app.version');
$b = exec('git rev-parse --short HEAD');
return ($v ? 'Version ' . $v : '') . ($b ? '<br>Build ' . $b : '');
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
protected static ?string $navigationGroup = 'Bookings';
protected static ?string $modelLabel = 'Booking';
protected static ?string $navigationLabel = 'Bookings';
protected static ?int $navigationSort = 1;
# ORDER BY with nullable columns
# In MySQL they will be placed before everything.
# But the intention of the application or the UX may need a different sort order.
# In these cases the ordering for NULL values can be changed easily.
-- Default behaviour: NULL values placed first
SELECT *
FROM customers ORDERBY country ASC;
-- NULL values placed first by rule
# https://sqlfordevs.com/delete-duplicate-rows
WITH duplicates AS (
SELECT id, ROW_NUMBER() OVER
(
PARTITION BY firstname, lastname, email
ORDER BY age DESC
) AS rownum
FROM contacts
)
SELECT t.my_column_1, t.my_column_2
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY my_column ORDER BY my_column_2 DESC) AS row_num
FROM my_table
) t
WHERE t.row_num = 1;