Skip to content

Instantly share code, notes, and snippets.

View Braunson's full-sized avatar

Braunson Yager Braunson

View GitHub Profile
@Braunson
Braunson / AppServiceProvider.php
Created July 23, 2021 16:31
Builder macro to output raw SQL with all bindings safely encoded with support for Eloquent Builder and Query Builder.
// Support for Query Builder
Illuminate\Database\Query\Builder::macro('toRawSql', function() {
return array_reduce($this->getBindings(), function ($sql, $binding) {
$binding = str_replace(['\\', "'"], ['\\\\', "\'"], $binding);
return preg_replace('/\?/', is_numeric($binding)
? $binding
: "'" . $binding . "'", $sql, 1);
}, $this->toSql());
});
@Braunson
Braunson / file.php
Created June 23, 2021 19:05
Laravel - Get information about a named route
$route = \Route::getRoutes()->getByName('home');
// => Illuminate\Routing\Route {
// +uri: "/",
// +methods: [
// "GET",
// "HEAD",
// ],
// +action: [
// "middleware" => [
@Braunson
Braunson / snippet.html
Created May 28, 2021 19:26
Alpine.js + Tailwind scroll to top snippet
<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 April 12, 2021 15:56
Why Laravel does not provide the authenticated user on any error pages

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 March 25, 2021 21:51
Laravel - Renaming Tables in Production

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 February 23, 2021 16:32
Install MeiliSearch in Laravel Homestead. Don't forget to port forward 7700 -> 7700 to be able to access the MeiliScript UI
#!/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 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