Skip to content

Instantly share code, notes, and snippets.

View AndreiTelteu's full-sized avatar

Andrei Telteu AndreiTelteu

View GitHub Profile

If you want to use bavix/laravel-wallet plugin with a one-time inline defined product, here is how to do it:

  1. Create a fake model for it

app/Models/InlineProduct.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

demo gif

/resources/views/vendor/backpack/crud/fields/dependent_text.blade.php

{{-- dependent text input --}}
@php
    $field['wrapper'] = $field['wrapper'] ?? $field['wrapperAttributes'] ?? [];
    $field['wrapper']['data-field-name'] = $field['wrapper']['data-field-name'] ?? $field['name'];
    $field['wrapper']['data-depends-on'] = json_encode($field['depends_on'] ?? []);
    $field['wrapper']['data-init-function'] = $field['wrapper']['data-init-function'] ?? 'bpFieldInitDependentTestElement';
@AndreiTelteu
AndreiTelteu / Axiom Logger for Laravel v10 .md
Created September 20, 2023 15:46
Axiom Logger for Laravel v10

app/Logging/AxiomLogger.php

<?php

namespace App\Logging;

use Http;
use Monolog\Logger;
use Monolog\Handler\HandlerInterface;
@AndreiTelteu
AndreiTelteu / linux-create-user-if-not-exists-hook.sh
Created July 2, 2023 14:54
Linux script create user and group if not exists
#!/bin/bash
USER_NAME=app
cat /etc/passwd | grep ${USER_NAME} >/dev/null 2>&1
if [ $? -eq 0 ] ; then
echo "user ${USER_NAME} exists"
else
groupadd -r ${USER_NAME} -g 1000 && useradd -u 1000 -r -g ${USER_NAME} -m -d /home/${USER_NAME} -s /bin/bash -c "App user" ${USER_NAME}
fi
@AndreiTelteu
AndreiTelteu / MySQL dynamic max_connections setting .md
Created May 8, 2023 19:44
MySQL Dynamic max_connections based on active threads

Put the following script in a .sh file, for example in /root/watch-mysql.sh

#!/usr/bin/bash
trap "exit" 0

MAXCONN=1000 # set this to whatever base max_connections you have defined in your my.conf setting
while true; do
  #mysql -e "SHOW VARIABLES LIKE 'max_connections';"
 THREADS=`mysqladmin status | awk '{print $4}'`
@AndreiTelteu
AndreiTelteu / React useBetterState with mutable, callback, and promise .md
Last active April 7, 2023 09:15
React useBetterState with mutable state, callback after change, and promise. Just like the old this.setState() Class Component days. DEMO: https://stackblitz.com/edit/react-usebetterstate

useBetterState

I'm trying to bring back the magic of setState(value, callback) from React Class Component days, while adding more functionality on top, like promises and mutable state.

To start using it, initialize it with:

const [state, setState] = useBetterState({
  count: 0,
@AndreiTelteu
AndreiTelteu / Incremental file backup and db dumps .md
Last active February 13, 2024 08:12
Incremental file backup and db dumps with mysqldump and autorestic, restic, sql, gzip, weekly, daily

Incremental file backup and MySQL backup

1. Install

Install autorestic: https://autorestic.vercel.app/installation

Let's use /root/backup to store our env file and autorestic config, make a data folder inside to store the actual backups inside, and a temporary database folder for MySQL dumps.

mkdir /root/backup
@AndreiTelteu
AndreiTelteu / Solidjs full year calendar .tsx
Created February 9, 2023 16:55
Solidjs full year calendar build with momentjs
import { Component, For } from "solid-js";
import moment from "moment";
import "moment/dist/locale/ro";
moment.locale("ro");
const App: Component = () => {
const weekdays = moment.weekdaysMin();
const months = moment.months();
const fullYear = Object.fromEntries(
months.map((monthName, monthIndex) => {
@AndreiTelteu
AndreiTelteu / Laravel stream large file with buffer .php
Created February 7, 2023 21:50
Laravel stream large file from disk with buffer.
<?php
// inspired by https://github.com/imanghafoori1/laravel-video
// if you need this for video, this does not support seek.
// laravel-video supports seek and chunks, so use that for video.
Route::get('/big-file-stream', function () {
$filePath = \Storage::disk('local')->path('video.mp4');
$fileStream = \Storage::disk('local')->readStream('video.mp4');

You can follow microsoft's official instalation steps, however if you have multiple php instalations (I have php7.4 and 7.3 installed on my laravel forge server) you need to specify what version to build the extension with.

# make sure you have this installed:
sudo apt-get install -y unixodbc-dev

# elevate as root
sudo su
pecl -d php_suffix=7.3 install sqlsrv
pecl -d php_suffix=7.3 install pdo_sqlsrv