Skip to content

Instantly share code, notes, and snippets.

View royteusink's full-sized avatar

Roy Teusink royteusink

View GitHub Profile
@royteusink
royteusink / spatie-laravel-data-value-cast.md
Created November 12, 2024 06:59
Spatie LaravelData nested value cast with dot notation
use Spatie\LaravelData\Casts\Cast;
use Spatie\LaravelData\Casts\IterableItemCast;
use Spatie\LaravelData\Support\Creation\CreationContext;
use Spatie\LaravelData\Support\DataProperty;

class ValueCast implements Cast, IterableItemCast
{
    public function __construct(
 protected ?string $value = null,
@royteusink
royteusink / ScopeWhereInSequence.md
Last active June 17, 2024 13:45
Laravel ScopeWhereInSequence

App/Traits/ScopeWhereInSequence.php

<?php

namespace App\Traits;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
@royteusink
royteusink / GenerateTypescriptRoutes.md
Last active November 19, 2024 19:15
Generate Laravel routes for typescript (SPA)

Laravel Command to generate route endpoints that can be used in typescript fully threeshakable.

GenerateTypescriptRoutes.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
@royteusink
royteusink / array-growsort.md
Last active February 15, 2024 15:01
Grow sort an array

Growsort

Maak een nieuwe array op basis van een groeisorteeralgoritme, begint bij de oorsprong en groeit vervolgens geleidelijk naar het begin en begin van de array.

Example use case: Loading images for a scroll animation sequence and start loading at a specific percentage

const imagesArray = [
    "image1.jpg",
    "image2.jpg",
@royteusink
royteusink / search_by_key_recursive.php
Created September 26, 2022 10:40
Search for a key in a nested array
<?php
function searchByKey(array $array, $key)
{
if (array_key_exists($key, $array)) return $array[$key];
$recursive = function($chunk) use ($key, &$recursive) {
foreach ($chunk as $k => $v) {
if ($k === $key) return $chunk[$key];
if (is_array($v) && $found = $recursive($v)) return $found;
@royteusink
royteusink / helpers.php
Last active August 25, 2022 14:26
Laravel PHP Unit test private and protected variable or function
<?php
if (!function_exists('testShadow')) {
/**
* Magically access a private or protected variable or methods from an instance of a class.
* Use this only in UnitTests
*
* @param mixed $instance Instance of a class
* @param string $name Name of the inaccessible variable or method
* @return mixed variable
@royteusink
royteusink / AppServiceProvider.php
Last active June 15, 2022 07:37
Laravel migration drop index if exists
<?php
// app/Providers/AppServiceProvider.php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
public function boot()
{
Blueprint::macro('dropIndexIfExists', function ($index) {
/** @var Blueprint $this */
@royteusink
royteusink / silverstripe-template-include-viewscripe.md
Last active October 29, 2021 09:40
SilverStripe template include viewscope unqiue id

Installation

composer require ramsey/uuid

app/src/TemplateHelpers.php

<?php
@royteusink
royteusink / vue-sanctum.md
Last active December 31, 2020 13:21
vue cli + laravel sanctum

CORS issues with local development setup Laravel Sanctum with Vue3 cli

app.yourdomain.test

vue app created with vue cli.

axios.create({
  baseURL: process.env.VUE_APP_API_URL,
  withCredentials: true
@royteusink
royteusink / add_smarty_to_laravel5.md
Last active February 20, 2019 10:09
Add Smarty Template to Laravel 5

Add (latest) Smarty Template to Laravel 5.

composer require smarty/smarty --prefer-dist dev-master

.gitignore

/storage/smarty