Skip to content

Instantly share code, notes, and snippets.

View Braunson's full-sized avatar

Braunson Yager Braunson

View GitHub Profile
@Braunson
Braunson / DirtyOverride.php
Created December 2, 2020 16:16
Laravel - Case your dirty attributes the same way they would be if they were using `getOriginal()`. Add this trait to your app and import it into the model you want to use it with.
<?php
namespace App\Traits;
trait DirtyOverride
{
/**
* Get the attributes that have been changed since last sync.
*
* @return array
@Braunson
Braunson / iseedAll.php
Created June 24, 2020 20:41
Command to use with the iSeed package to get tables, generated the seeds for all tables in one command `php artisan iseed:all`
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class iseedAll extends Command
{
/**
* The name and signature of the console command.
*
@Braunson
Braunson / phpmyadmin.sh
Last active June 24, 2020 19:43
Slightly modified version of https://raw.githubusercontent.com/grrnikos/pma/master/pma.sh to support one argument, the URL to serve the site on.
#!/bin/bash
LATEST_VERSION=$(curl -sS 'https://api.github.com/repos/phpmyadmin/phpmyadmin/releases/latest' | awk -F '"' '/tag_name/{print $4}')
DOWNLOAD_URL="https://api.github.com/repos/phpmyadmin/phpmyadmin/tarball/$LATEST_VERSION"
SERVE_URL=${1:-phpmyadmin.local}
echo "Downloading phpMyAdmin $LATEST_VERSION"
wget $DOWNLOAD_URL -q -O 'phpmyadmin.tar.gz'
mkdir phpmyadmin && tar xf phpmyadmin.tar.gz -C phpmyadmin --strip-components 1
@Braunson
Braunson / binary-trees-as-arrays.php
Last active December 8, 2021 02:42
Dealing with binary trees represented as arrays and determining weather the left or right branch of the tree is larger. The size of each branch is the sum of the node values. (`-1` is considered a non-existent node)
<?php
function solution($arr)
{
// If the array is empty or there is only 1 value
if (count($arr) <= 1) {
return '';
}
// A quick check as to not redefine the function
@Braunson
Braunson / CarbonBirthday.php
Last active December 19, 2019 21:04
Carbon macro for checking if a birthday is between two dates (i.e. is the birthday upcoming within the next 3 months)
<?php
// Datermines if the birth date given is between two dates.
Carbon::macro('isBirthdayBetween', function($start, $end) {
// Adjust the birthdate year to the current year
$birthday = $this->copy()->year(date('Y'));
// If the birthday has past, add a year (nearing the new year)
if ($birthday->isPast()) {
$birthday = $birthday->addYear();
@Braunson
Braunson / pivot-tables.md
Last active May 15, 2024 08:12
Laravel 8.x - Diving into Pivot Tables

Laravel 6 - Diving Into Pivot Tables

Pivot tables can be confusing and a little hard to wrap your head around at first. In this quick article we are going to dive into what a pivot table is, how to create one and finally how to use the pivot table. Let's dive in!

What is a pivot table?

A pivot table is used to connect relationships between two tables. Laravel provides a Many To Many relationship where you can use a pivot table.

@Braunson
Braunson / snippet.php
Last active October 24, 2019 20:40
Laravel Collection Macro trimEndWhile
Collection::macro('trimEndWhile', function ($callback) {
$collection = new static($this->items);
while ($collection->count() && $callback($collection->last()) {
$collection->pop();
}
return $collection;
});
@Braunson
Braunson / CollectCodeCoverage.php
Created August 15, 2019 13:52
app/Http/Middleware/CollectCodeCoverage.php + tests/Feature/xCoverage.php Test code coverage middleware for Laravel 5.8+
<?php
namespace App\Http\Middleware;
use Closure;
class CollectCodeCoverage {
/**
* Used for code coverage when running unit tests
@Braunson
Braunson / gist.md
Created March 2, 2018 22:24
Laravel Mix - Making functions and variables accessible in the global scope/object

To explain why to use the window. notation a little more (because I was confused at some point with it too) Webpack wraps your code in functions to create scope, so that each file can act as an independent module. This keeps your various modules from polluting the global scope or each other and also means test is not available to the browser console. If you just want to call your test function from the browser console try assigning test to the window object.

There's a great answer at SO on options with several approaches for globals here https://stackoverflow.com/a/40416826/610880

@Braunson
Braunson / jquery.google-autocomplete.js
Created February 4, 2018 19:46
jQuery Wrapper for @dericcain's google-address-autocomplete
/*!
* jQuery Google autocomplete plugin
* Description: THis is just a jQuery wrapper for @dericcain's method helper linked below
* Author: @braunson
* Requirements: https://github.com/dericcain/google-address-autocomplete
* Licensed under the MIT license
*/;
(function($) {
// Usage $('[data-google-autocomplete]').googleAutocomplete();