Skip to content

Instantly share code, notes, and snippets.

@andyg1
andyg1 / gist:7459f11e72b536a692829cbc6e3ede2e
Created September 29, 2023 20:13
Show running service in linux
service --status-all
@andyg1
andyg1 / gist:a60a1c89048d80f789297368645d7b5b
Created September 29, 2023 20:12
Check if a service is running in Linux
# lists all running processes and filters them by name (in this example "dbus")
ps -ef | grep dbus
@andyg1
andyg1 / Sensible database naming conventions.md
Last active September 11, 2023 15:53
Sensible database naming conventions

Sensible database naming conventions

Taken from this article:

  • Primary keys are always named id whatever the data type is.
  • Timestamps are past tense, suffixed …_at
  • Date, Time, and Datetime are not timestamps, name them freely.
  • Booleans are always positive and start with is_…
  • Numeric columns should be plural, like guesses or failures
  • Lists as JSON columns are plural, and may end with 1…_list1
  • Complex JSON trees can be named freely, but may end with …_tree
@andyg1
andyg1 / gist:84b964d81aeeb9c418890fdeb20fb855
Created August 25, 2023 08:38
Laravel - convert an array including its children to Collections
<?php
// source: https://laravel-code.tips/add-a-recursive-macro-to-convert-an-array-including-its-children-to-collections/
// Pop this in boot() of a service provider
Collection::macro('recursive', function () {
return $this->map(function ($value) {
if (is_array($value) || is_object($value)) {
return collect($value)->recursive();
}
<?php
function objectToArray($object) {
$json = json_encode($object);
return json_decode($json, true);
}
// or, in one line
@andyg1
andyg1 / gist:ad378288846dfc9bff673db018e5afa2
Created April 25, 2023 10:22
Import a sql dump into a mysql docker container
# from the host terminal
# find the container id
$ docker ps
# copy the file
$ docker cp /path/to/file.sql <CONTAINTER_ID>:/file.sql
Variables => camelCase => $articlesWithAuthor
Collection Variable => descriptive, plural => $activeUsers = User::active()->get()
Object Variable => descriptive, singular => $activeUser = User::active()->first()
View => snake_case => show_filtered.blade.php
Controllers => singular, ProperCase => ArticleController
Model Name => singular, ProperCase => User, FollowingRequest
Model attribute/property => snake_case => $model->created_at
Method => camelCase => getAll
Method in resource controller => table => store
Method in test class => camelCase => testGuestCannotSeeArticle
@andyg1
andyg1 / MigrationController.php
Last active April 26, 2018 15:48
Working with Laravel Migrations on servers without shell access
<?php
// app/Http/Controllers/MigrationController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
class MigrationController extends Controller
{
git show --pretty="" --name-only COMMIT_REFERENCE
// as answer here: https://stackoverflow.com/questions/927358/how-to-undo-the-most-recent-commits-in-git
$ git commit -m "Something terribly misguided" (1)
$ git reset HEAD~ (2)
<< edit files as necessary >> (3)
$ git add ... (4)
$ git commit -c ORIG_HEAD (5)
/******