Skip to content

Instantly share code, notes, and snippets.

View Braunson's full-sized avatar

Braunson Yager Braunson

View GitHub Profile
@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 August 13, 2023 19:25
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();
@Braunson
Braunson / migration_example.php
Created December 21, 2017 18:21
Need to loop through multiple Laravel models to do the same update on all of their results? In this example it's updating the address country id
<?php
// Models to run through
$models = collect([
'contact',
'business',
]);
// Loop through the models
$models->each(function ($value, $key) use ($keys) {
@Braunson
Braunson / searchreplacedb2.php
Created October 23, 2017 02:05
interconnectit/Search-Replace-DB 2.0.1 - Migrated from the MySQL Extension to PDO
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
/**
*
* Safe Search and Replace on Database with Serialized Data v2.0.1
*
* This script is to solve the problem of doing database search and replace when
* developers have only gone and used the non-relational concept of serializing
* PHP arrays into single database columns. It will search for all matching
layout title permalink
checklist_page
The Side Project Marketing Checklist
/marketing-checklist/

Pre-Launch

Market Research

@Braunson
Braunson / pwd-faas.md
Created January 11, 2017 02:55 — forked from alexellis/pwd-faas.md
pwd-faas-quickstart.md

This is a Quickstart guide for FaaS functions as a Service on Play-with-docker.com

Head over to http://play-with-docker.com/ and start a new session. Add one host to start with.

# docker swarm init # use --advertise-addr here and pick the 10.x IP address range

# git clone https://github.com/alexellis/faas
# cd faas
# ./deploy_stack.sh