Skip to content

Instantly share code, notes, and snippets.

View nullthoughts's full-sized avatar

Jani Gyllenberg nullthoughts

View GitHub Profile
@nullthoughts
nullthoughts / MacroServiceProvider.php
Last active December 1, 2023 21:59
Convert Laravel Scout query to Eloquent query (Macro)
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Laravel\Scout\Builder;
class MacroServiceProvider extends ServiceProvider
{
/**
@nullthoughts
nullthoughts / DuskServiceProvider.php
Last active February 4, 2023 21:45
Mock classes & routes in Laravel Dusk tests
<?php
namespace App\Providers;
class DuskServiceProvider extends ServiceProvider
{
/**
* The classes that should be mocked for Dusk tests
*
* @var array
@nullthoughts
nullthoughts / PipelineFilter.php
Created September 8, 2021 20:20
Pipeline extended with second parameter (with data)
<?php
namespace App\Filters;
use Closure;
use Illuminate\Pipeline\Pipeline;
use Throwable;
class PipelineFilter extends Pipeline
{
@nullthoughts
nullthoughts / scopeBySaleType.php
Created October 23, 2019 14:04
scopeBySaleType
public function scopeBySaleType($query, type)
{
$query->whereHas('sales', function ($query) use ($type) {
$query->where('id', function ($sub) {
$sub->from('sales')
->selectRaw('max(id)')
->whereColumn('sales.inventory_id', 'inventories.id');
})->where('type', $type);
});
}
@nullthoughts
nullthoughts / helpers.php
Created September 7, 2019 21:50
Lode app console() method helper for Laravel
if(! class_exists(\LodeApp\PHPUnit\Console::class)) {
function console($vars, $dump = true) {
dump($vars);
}
}
@nullthoughts
nullthoughts / LockColumns.php
Created May 2, 2019 16:35
Lock columns/attributes from updates in Laravel
<?php
namespace App\Traits;
trait LockColumns
{
/**
* Cast locked_columns as a JSON column
*
* @return void
@nullthoughts
nullthoughts / CorsHeaders.php
Created March 7, 2019 20:32
Laravel CORS Middleware
<?php
namespace App\Http\Middleware;
use Closure;
class CorsHeaders
{
/**
* Handle an incoming request.
@nullthoughts
nullthoughts / AdvancedAlgoliaEngine.php
Created January 16, 2019 17:48
Custom Algolia Engine for Laravel to only index updated models
<?php
namespace App\Engines;
use Illuminate\Support\Collection;
use Laravel\Scout\Engines\AlgoliaEngine;
class AdvancedAlgoliaEngine extends AlgoliaEngine
{
@nullthoughts
nullthoughts / batchRename.php
Created September 19, 2018 21:42
Batch rename/move files with callback function
public static function testRename()
{
self::batchRename('s3', 'documents', function ($file) {
return str_replace('%20', '-', $file);
});
}
public static function batchRename($disk, $directory, $callback)
{
$disk = \Illuminate\Support\Facades\Storage::disk($disk);
@nullthoughts
nullthoughts / SimpleXMLExtended.php
Created August 11, 2018 17:09
Extends SimpleXMLElement to support writing CDATA
<?php
class SimpleXMLExtended extends SimpleXMLElement
{
// Inspired by: https://stackoverflow.com/questions/6260224/how-to-write-cdata-using-simplexmlelement/6260295
public function addCDATA($string)
{
$node = dom_import_simplexml($this);
$nodeOwner = $node->ownerDocument;