Skip to content

Instantly share code, notes, and snippets.

View nullthoughts's full-sized avatar

Jani Gyllenberg nullthoughts

View GitHub Profile
@nullthoughts
nullthoughts / mergeJson.php
Created July 12, 2018 19:54
Laravel helper for merging two Json files matched by key value
public static function mergeJson($to, $from, $matchBy, $key)
{
$to = json_decode(file_get_contents(storage_path($to)));
$from = json_decode(file_get_contents(storage_path($from)));
$values = [];
foreach($from as $object) {
$values[$object->{$matchBy}] = $object->{$key};
}
@nullthoughts
nullthoughts / vueImplode.js
Created August 3, 2018 18:53
Vue Implode Method (Join on Array Key)
implode(array, key = null, glue = ', ') {
if(key) {
array = array.map(function(element) {
return element[key];
})
}
return array.join(glue);
},
@nullthoughts
nullthoughts / str_between.php
Last active August 23, 2018 13:27
PHP function to return string between two strings
/**
* Get the portion of a string between two given strings.
*
* @param string $subject
* @param string $start
* @param string $end
* @param bool $insensitive
* @param bool $innerOnly
* @param bool $inverse
*
@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 / 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 / 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 / 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 / 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 / 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 / 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;