Skip to content

Instantly share code, notes, and snippets.

@kmuenkel
kmuenkel / TestCase.php
Last active January 3, 2022 01:29
Testbench bug workaround
<?php
namespace Tests;
use Throwable;
use PHPUnit\Util\Test;
use BadMethodCallException;
use Illuminate\Http\Request;
use Faker\Generator as Faker;
use Illuminate\Routing\Router;
@kmuenkel
kmuenkel / artisan.php
Created June 21, 2021 20:58
Artisan command for Composer packages
#!/usr/bin/env php
<?php
use Illuminate\Config\Repository;use Illuminate\Encryption\Encrypter;define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
@kmuenkel
kmuenkel / AppServiceProvider.php
Last active November 4, 2021 16:46
Register Service Providers for 'require-dev' packages only if 'composer install' was run without the '--no-dev' argument.
<?php
namespace App\Providers;
class AppServiceProvider
{
public function register()
{
$this->registerDev()
}
@kmuenkel
kmuenkel / AssociateModels.php
Last active November 4, 2021 16:45
Connect two models regardless of relationship type
<?php
if (!function_exists('associate_models')) {
/**
* @param Model $model
* @param Model $relatedModel
* @param string $relationName
*/
function associate_models(Model $model, Model $relatedModel, string $relationName)
{
<?php
namespace App\Http\Middleware;
use Closure;
use Exception;
use Illuminate\Http\{JsonResponse, RedirectResponse, Request, Response};
/**
* Class Debug
@kmuenkel
kmuenkel / AppServiceProvider.php
Last active August 6, 2021 20:01
Correct for non-UTF-8 characters in response arrays prior to JSON conversion
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Overrides\Illuminate\Routing\ResponseFactory;
use Illuminate\Contracts\View\Factory as ViewFactoryContract;
use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract;
/**
@kmuenkel
kmuenkel / Debug.php
Created August 16, 2021 18:36
Debug log data about incoming requests and responses
<?php
namespace App\Http\Middleware;
use Closure;
use Throwable;
use GuzzleHttp\Psr7\Uri;
use Illuminate\Http\{JsonResponse, RedirectResponse, Request, Response};
/**
@kmuenkel
kmuenkel / AttributeCamelCase.php
Last active December 23, 2021 21:34
Expand on Eloquent's attribute handling for camel-cased properties on snake-cased fields, and enum mutations
<?php
namespace App\Models\Entities;
use Str;
trait AttributeCamelCase
{
/**
* @param string $key
@kmuenkel
kmuenkel / AccountScopes.php
Created October 1, 2021 03:41
Passport scopes with abstracted route names
<?php
namespace App\Models\Data;
use BadMethodCallException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
/**
* @method static string accountRetrieval()
* @method static string accountCreation()
<?php
namespace Database\Seeders;
use Illuminate\Support\Arr;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\{DB, Schema};
/**
* @property string|Model $model