Skip to content

Instantly share code, notes, and snippets.

Avatar

Braunson Yager Braunson

View GitHub Profile
@Braunson
Braunson / file.php
Created Jun 23, 2021
Laravel - Get information about a named route
View file.php
$route = \Route::getRoutes()->getByName('home');
// => Illuminate\Routing\Route {
// +uri: "/",
// +methods: [
// "GET",
// "HEAD",
// ],
// +action: [
// "middleware" => [
@Braunson
Braunson / snippet.html
Created May 28, 2021
Alpine.js + Tailwind scroll to top snippet
View snippet.html
<div x-data="{scrollBackTop: false}" x-cloak>
<button
x-show="scrollBackTop"
x-on:scroll.window="scrollBackTop = (window.pageYOffset > window.outerHeight * 0.5) ? true : false"
x-on:click="$scroll('#top')"
aria-label="Back to top"
class="fixed bottom-0 right-0 p-2 mx-10 my-10 text-white bg-gray-800 hover:bg-gray-700 focus:outline-none">
<svg viewBox="0 0 20 20" fill="currentColor" class="w-6 h-6">
<path fill-rule="evenodd" d="M14.707 12.707A1 1 0 01-1.414 0L10 9.414l-3.293 3.293a1 1 0 01-1.414-1.414l4-4a1 1 0 011.414 0l4 4a1 1 0 010 1.414z" clip-rule="evenodd"></path>
</svg>
@Braunson
Braunson / text.md
Created Apr 12, 2021
Why Laravel does not provide the authenticated user on any error pages
View text.md

I was curious why my authenticated user didn't persist to my error pages in Laravel.

Only noticing as the layout used for the error pages (header/footer) is the same as the rest of the site and the top right shows the authenticated user, shows a user nav, logout, etc.

Doing a quick Google search brought up some solutions but not necessarily a reason why authenticated users are not available on the error pages.

The reason not to have the authenticated user on the error page:

  • What if the issue is with fetching the authenticated user?
@Braunson
Braunson / laravel-renaming-tables-in-production.md
Last active Mar 25, 2021
Laravel - Renaming Tables in Production
View laravel-renaming-tables-in-production.md

Laravel - Renaming Tables in Production

So I ran into a use-case where I joined a project that had a prefix on all tables in a production database. Now I couldn't just drop all tables and migrate + seed, this is Production we are talking about!

Since the database had a second (much smaller) app's tables in the same database, I had to be careful that I didn't rename those either.

Here's a quick script I threw in a developer-only route which would generate a query which I could manually run on production at the time of deploying the new code which was stripped of hard coded prefixes. Don't forget when you run this to remove any prefixes set for your connection in config/database.php otherwise you'll have some angry customers and "unable to find table" errors.

@Braunson
Braunson / install-meilisearch.sh
Created Feb 23, 2021
Install MeiliSearch in Laravel Homestead. Don't forget to port forward 7700 -> 7700 to be able to access the MeiliScript UI
View install-meilisearch.sh
#!/bin/bash
echo "Downloading MeiliSearch"
#
# Install MeiliSearch
# https://docs.meilisearch.com/create/how_to/running_production.html#step-2-run-meilisearch-as-a-service
#
# Update the list of available packages and their versions
@Braunson
Braunson / DirtyOverride.php
Created Dec 2, 2020
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.
View DirtyOverride.php
<?php
namespace App\Traits;
trait DirtyOverride
{
/**
* Get the attributes that have been changed since last sync.
*
* @return array
@Braunson
Braunson / iseedAll.php
Created Jun 24, 2020
Command to use with the iSeed package to get tables, generated the seeds for all tables in one command `php artisan iseed:all`
View iseedAll.php
<?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 Jun 24, 2020
Slightly modified version of https://raw.githubusercontent.com/grrnikos/pma/master/pma.sh to support one argument, the URL to serve the site on.
View phpmyadmin.sh
#!/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 Feb 10, 2020
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)
View binary-trees-as-arrays.php
<?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 Dec 19, 2019
Carbon macro for checking if a birthday is between two dates (i.e. is the birthday upcoming within the next 3 months)
View CarbonBirthday.php
<?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();