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 January 6, 2022 22:47
Laravel macro for Request class when needing to specify whenFilled but only for query params.
<?php
// Only when the query param is filled. Using whenFilled will check everything, not just query params
Request::macro('whenQueryFilled', function ($key, callable $callback, callable $default = null) {
if (! is_null($this->retrieveItem('query', $key, null))) {
return $callback(data_get($this->query(), $key)) ?: $this;
}
if ($default) {
return $default();
@Braunson
Braunson / AppServiceProvider.php
Created January 6, 2022 21:27
Detect if a request is from Livewire or not, useful when you have middleware that needs to ignore Livewire requests. Drop this in the boot method.
<?php
// Detect requests from Livewire
Request::macro('isFromLivewire', function() {
return $this->headers->has('x-livewire');
});
@Braunson
Braunson / functions.php
Created November 21, 2021 21:49
Dump all callbacks for a filter/hook in WordPress
function list_hooks( $hook = '' ) {
global $wp_filter;
if ( isset( $wp_filter[$hook]->callbacks ) ) {
array_walk( $wp_filter[$hook]->callbacks, function( $callbacks, $priority ) use ( &$hooks ) {
foreach ( $callbacks as $id => $callback )
$hooks[] = array_merge( [ 'id' => $id, 'priority' => $priority ], $callback );
});
} else {
return [];
@Braunson
Braunson / AppServiceProvider.php
Created November 11, 2021 19:36
whereBetweenDates macro scope for Laravel 8x.
<?php
public function boot()
{
// Extend the Query Builder
\lluminate\Database\Query\Builder::macro('whereBetweenDates', function($firstColumn, $secondColumn, $firstDate, $secondDate, $firstComparison = '>=', $secondComparison = '<=') {
return $this
->whereDate($firstColumn, $firstComparison, $firstDate)
->whereDate($secondColumn, $secondComparison, $secondDate);
});
@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