Skip to content

Instantly share code, notes, and snippets.

View chasecmiller's full-sized avatar

Crumbls chasecmiller

View GitHub Profile
@chasecmiller
chasecmiller / gist:2b0a1dbfb9d92ad4cc66b1364db86726
Created August 28, 2023 16:54
Sharing resources between tenants in Filanent.
/**
* On your resource, you need to override the getEloquentQuery method. Here is the original:
**/
public static function getEloquentQuery(): Builder {
$query = static::getModel()::query();
if ($tenant = Filament::getTenant()) {
static::scopeEloquentQueryToTenant($query, $tenant);
}
@chasecmiller
chasecmiller / notes.md
Created June 29, 2023 00:17
Adding middleware to specific resource pages in Filament.

Adding middleware to specific resource pages in Filament.

Add this code to your resource class.

/**
 * Bring our routes online.
 * @return Closure
 */
public static function getRoutes(): Closure	{
	return function () {
@chasecmiller
chasecmiller / Password.php
Last active June 7, 2022 05:44
Laravel Rule encouraging Stanford's Password Policy
<?php
namespace App\Rules;
use Illuminate\Validation\Rules\Password as Rule;
/**
* A password rule for Laravel based on Stanford's best practices.
* https://uit.stanford.edu/service/accounts/passwords
*
@chasecmiller
chasecmiller / CheckSsl.php
Created December 20, 2021 06:41
Check a domain's SSL expiration via a Laravel command
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
/**
* In no way is this complete, but should give you a good starting point.
**/
@chasecmiller
chasecmiller / AppServiceProvider.php
Created November 23, 2021 16:11
Share user to all views in Laravel
/**
* This is a dead simple way to share your current user within all views in a traditional website.
* Make sure you are importing Illuminate\Support\Facades\View into your AppServiceProvider,
* then add this to the boot method. Once you do that, you can use the $user variable at
* any point in the view to get your currently authenticated view.
*/
View::share('user', \Auth::user());
@chasecmiller
chasecmiller / Get-All-Laravel-Models.php
Created September 13, 2021 04:18
A simple way to get all models in your app space in Laravel.
$files = [];
foreach ([
app_path('Models/*.php'),
app_path('*.php'),
] as $path) {
$folder = dirname($path);
if (!file_exists($folder) || !is_dir($folder)) {
continue;
}
$files = array_merge($files, glob($path));
@chasecmiller
chasecmiller / gist:027896f5518d44141687cca4da4c10b7
Last active March 5, 2021 19:08
Getting the row number for the current db entry in Laravel.
This is a very inefficient way to do it once the db is huge. But it works. For the example, the user id is 5.
$userId = 5;
\DB::statement('SET @row=0');
$user = \App\Models\User::whereRaw('1=1')
->select([
\DB::raw('@row:=@row+1 as row'),
\DB::raw('count(*) as c'),
'users.*'
])
@chasecmiller
chasecmiller / HasPasswordAttribute.php
Last active February 3, 2021 17:24
A laravel trait for setting passwords with automatic hashing.
<?php
namespace App\Traits;
use Illuminate\Support\Facades\Hash;
/**
* Trait HasPasswordAttribute
* @package App\Traits
*/
@chasecmiller
chasecmiller / HasExtendedData.php
Created January 22, 2020 15:38
A Laravel trait to allow any data to be added to a model .
<?php
// Customize this.
namespace Crumbls\ExtendedData\Traits;
use Str;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
@chasecmiller
chasecmiller / wpeCacheFix.php
Created January 11, 2018 18:20
WPEngine - Cache key override - You need this if you want to run a secondary ( or multisite ) in a subdirectory on WP Engine
<?php
/**
* Author: Chase C. Miller
* Working Date: 2017-01-11
* Custom cache key for WPEngine to bypass object caching problems that can happen when you install a secondary or multisite on WPEngine.
* It should be installed as a mu-plugin to catch early activation.
**/
global $wp_object_cache;