Skip to content

Instantly share code, notes, and snippets.

View patoui's full-sized avatar
😃
Trying to write code my future self will be proud of

Patrique Ouimet patoui

😃
Trying to write code my future self will be proud of
View GitHub Profile
@patoui
patoui / nativephp-linux-patch.sh
Created July 21, 2023 13:32
Script to fix issues when running NativePHP on Linux
#!/bin/bash
# when running `php artisan native:install`, when prompted to start the development server select "no"
# copy this file within the root of your project/application and run it `bash nativephp-linux-patch.sh`
# after running this script run `php artisan native:serve`
sed -i 's/app.dock.setIcon(state_1.default.icon);/\/\/app.dock.setIcon(state_1.default.icon);/g' vendor/nativephp/electron/resources/js/node_modules/nativephp-electron/dist/index.js
cp -L /usr/bin/php vendor/nativephp/php-bin/bin/mac/x86/php
@patoui
patoui / prototype_pollution_example.js
Last active July 13, 2023 15:45
Prototype pollution example
// see https://learn.snyk.io/lesson/prototype-pollution/ for additional information and great explanations
// recursively update target with source data
function merge(target, source) {
for (const attr in source) {
if (
typeof target[attr] === "object" &&
typeof source[attr] === "object"
) {
merge(target[attr], source[attr])
##### dataCount: 10000, seletCount: 1, limit: 5000 #####
PhpClickhouse 0.076
SeasClickNonCompression 0.062
SeasClickCompression 0.064
OneCk 0.095
--------------------------------
Total 0.297
##### dataCount: 10000, seletCount: 1, limit: 5000 #####
PhpClickhouse 0.051
## Introduction
This post aims to provide a simple example of how to use traits with Laravel Query Scopes. The trait will sort columns based on "sort" and "direction" being found within the request, while protecting the database of unwanted sorting by adding a "sortables" property to the model. The end result looks like this with a Post model as an example:
```
Post::sort(request())
```
### Sortable trait
@patoui
patoui / temp_secrets.php
Last active July 21, 2022 13:27
Temporary/short lived secrets script
<?php
declare(strict_types=1);
/**
* Tested with PHP 8.1
* Create a sqlite file named `secrets.db` in the same directory
* Required extensions: sqlite3/pdo_sqlite, openssl
* Recommended extension: uuid
*
<?php
declare(strict_types=1);
/**
* src/
* Domain/
* Entity/
* Factory/
* Repository/ // repository interfaces
@patoui
patoui / HasManyUpdator.php
Last active February 7, 2020 13:25
Utility class to perform multiple updates on a has many relationship (thanks to Glen UK on Larachat for finding bugs)
<?php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\RelationNotFoundException;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class HasManyUpdater

Laravel Eloquent vs Collection Count

This can be a little confusing as the syntax barely changes but it makes a big difference.

Using Collection "count"

This seems harmless, but I'll explain why it can be bad for performance

$post-&gt;comments-&gt;count();
@patoui
patoui / namespace-before-action-sample.rb
Created December 8, 2018 17:17
Applying before_action to entire namespace sample
# config/routes.rb
namespace :admin do
get 'dashboard' => 'dashboard#index'
end
# app/controllers/admin/admin_controller.rb
include SessionsHelper
class Admin::AdminController < ApplicationController
http_basic_authenticate_with name: "johndoe", password: "secret"
@patoui
patoui / ruby-on-rails-exploration-part-2.md
Last active December 5, 2018 00:25
ruby-on-rails-exploration-part-2

Introduction

Part 2! This is an overview of things I've learnt while working on my side project Anon Forum in Ruby on Rails.

Today's topics:

  • Custom Validation
  • Pagination

Custom Validation