Skip to content

Instantly share code, notes, and snippets.

Avatar

Jani Gyllenberg nullthoughts

View GitHub Profile
@nullthoughts
nullthoughts / mergeJson.php
Created Jul 12, 2018
Laravel helper for merging two Json files matched by key value
View mergeJson.php
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 Aug 3, 2018
Vue Implode Method (Join on Array Key)
View vueImplode.js
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 Aug 23, 2018
PHP function to return string between two strings
View str_between.php
/**
* 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 / SimpleXMLExtended.php
Created Aug 11, 2018
Extends SimpleXMLElement to support writing CDATA
View SimpleXMLExtended.php
<?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;
@nullthoughts
nullthoughts / batchRename.php
Created Sep 19, 2018
Batch rename/move files with callback function
View batchRename.php
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 Jan 16, 2019
Custom Algolia Engine for Laravel to only index updated models
View AdvancedAlgoliaEngine.php
<?php
namespace App\Engines;
use Illuminate\Support\Collection;
use Laravel\Scout\Engines\AlgoliaEngine;
class AdvancedAlgoliaEngine extends AlgoliaEngine
{
@nullthoughts
nullthoughts / CorsHeaders.php
Created Mar 7, 2019
Laravel CORS Middleware
View CorsHeaders.php
<?php
namespace App\Http\Middleware;
use Closure;
class CorsHeaders
{
/**
* Handle an incoming request.
@nullthoughts
nullthoughts / LockColumns.php
Created May 2, 2019
Lock columns/attributes from updates in Laravel
View LockColumns.php
<?php
namespace App\Traits;
trait LockColumns
{
/**
* Cast locked_columns as a JSON column
*
* @return void
@nullthoughts
nullthoughts / helpers.php
Created Sep 7, 2019
Lode app console() method helper for Laravel
View helpers.php
if(! class_exists(\LodeApp\PHPUnit\Console::class)) {
function console($vars, $dump = true) {
dump($vars);
}
}
View scopeBySaleType.php
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);
});
}