Skip to content

Instantly share code, notes, and snippets.

View blood72's full-sized avatar

Kim Dong-Hyeon blood72

  • @marketboro 마켓보로
  • South Korea
  • 03:23 (UTC +09:00)
View GitHub Profile
@blood72
blood72 / array_column_recursive.php
Last active April 12, 2021 18:38
Return the values from a single column in the input array recursively
<?php
if (! function_exists('array_column_recursive')) {
/**
* Return the values from a single column in the input array recursively
*
* @param array $array
* @param int|string|null $columnKey
* @param int|string|null $indexKey
* @return array
@blood72
blood72 / version.php
Last active July 8, 2021 08:36
Get the closest version to a given value & Sort to version
<?php
if (! function_exists('version_sort')) {
/**
* Sort array with version_compare.
* @link https://stackoverflow.com/a/48974986/5067386
* @param string[] $versions
* @return void
*/
function version_sort(array $versions) {
@blood72
blood72 / DefaultGuardChangeDynamically.php
Created October 29, 2020 12:57
Change Auth default guard dynamically
<?php
namespace App\Http\Middleware;
use Closure;
class DefaultGuardChangeDynamically
{
/**
* Handle an incoming request.
@blood72
blood72 / HasFactory.php
Last active October 10, 2020 11:28
Guess factory class name & path based on class_basename() in Laravel 8
<?php
namespace App\Concerns;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory as BaseTrait;
trait HasFactory
{
use BaseTrait;
@blood72
blood72 / Example.php
Created October 7, 2020 04:35
Load Eloquent directly from DB stored in json format snapshot data.
<?php
namespace App\Models;
class Example extends Model
{
/**
* @see Post
* @param string $value
* @return Post|\Illuminate\Database\Eloquent\Model
@blood72
blood72 / RestrictFromReferer.php
Last active January 21, 2021 13:56
(Laravel) Allow request only if Referrer header is base same domain
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
class RestrictFromReferer
{
@blood72
blood72 / 2020_07_24_create_attachments_table.php
Created August 1, 2020 03:22
Attachment model (personal use)
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateAttachmentsTable extends Migration
{
/**
* Run the migrations.
@blood72
blood72 / PreferredLanguage.php
Last active July 17, 2020 15:38
Laravel middleware that changes to user preferred language (without route prefix).
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class PreferredLanguage
{
@blood72
blood72 / Permission404Middleware.php
Created June 29, 2020 07:11
404 middleware version of spatie/laravel-permission.
<?php
namespace App\Http\Middleware;
use Closure;
use Spatie\Permission\Middlewares\PermissionMiddleware as Middleware;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class Permission404Middleware extends Middleware
{
@blood72
blood72 / FakerGenerator.php
Last active May 21, 2020 16:14
Add randomBody method in Faker\Generator class
<?php
namespace App\Packages;
use Faker\Generator;
/**
* @method string randomBody($maxDepth = 4, $maxWidth = 4)
*/
class FakerGenerator extends Generator