Skip to content

Instantly share code, notes, and snippets.

View mykeels's full-sized avatar
😄
faffing!

Michael Ikechi mykeels

😄
faffing!
View GitHub Profile
@mykeels
mykeels / Laravel-Content.md
Created August 5, 2018 11:25
Extending Models with Content and Content-Type

Extending Models with Content and Content-Type

Traditional relational databases have structure, so we use migrations to manage that structure in Laravel.

This is cool, until you find yourself creating tables for things that you don't need tables for, because none of the tables' columns act as foreign keys to keys on other tables.

E.g. a user_settings table would have a structure like

id user_id key value

Usage

  • For Laravel Applications
    • Place the PHP script in app/Console/Commands/ directory
  • Run php artisan make:filter {FilterName} where {FilterName} is the name of the filter you intend to create.
@mykeels
mykeels / array.prototype.js
Created April 22, 2018 19:14
taboo prototype functions you should NOT use
Array.prototype.paginate = function (length) {
var arr = this;
length = (length || 1);
return arr.reduce(function (a, b) {
if (!a[a.length - 1] || a[a.length - 1].length == length) a.push([]);
a[a.length - 1].push(b);
return a;
}, []);
}
@mykeels
mykeels / 2018_04_17_000000_sample_migration.php
Created April 17, 2018 09:54
A sample laravel migration class using the [SampleMigration](https://gist.github.com/mykeels/b6cc690012ca11063d0303ee2daa08bd) model that implements the [ModelTableNameTrait](https://gist.github.com/mykeels/df13807cf105d0f92d6d4eaa3fa22d5b)
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\Models\SampleMigration;
class SampleMigration extends Migration
{
/**
* Run the migrations.
@mykeels
mykeels / SampleMigration.php
Last active April 17, 2018 09:49
A sample laravel model
<?php
namespace App\Models;
use App\Traits\ModelTableNameTrait;
class SampleMigration
{
use ModelTableNameTrait;
@mykeels
mykeels / ModelTableNameTrait.php
Created April 17, 2018 09:44
A trait that adds a static `name()` method to Laravel Models
<?php
namespace App\Traits;
trait ModelTableNameTrait
{
public static function name() {
return with(new static)->getTable();
}
}
@mykeels
mykeels / 2018_04_17_000000_sample_migration.php
Created April 17, 2018 09:29
A sample migration php class generated by Laravel
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class SampleMigration extends Migration
{
/**
* Run the migrations.
@mykeels
mykeels / states-and-cities.json
Created April 8, 2018 12:43
Nigerian States and Cities in JSON
[
{
"name": "Abia",
"cities": [
"Aba South",
"Arochukwu",
"Bende",
"Ikwuano",
"Isiala Ngwa North",
"Isiala Ngwa South",
const storage = {}
app.post('/', textBodyParser, (req, res) => {
storage[req.hash] = storage[req.hash] || req.body.split('\n').map((line) => line.split('').reverse().join('')).join('\n')
res.send(storage[req.hash])
})
const express = require('express')
const crypto = require('crypto')
const isPrime = require('./is-prime')
const app = express()
const calculateHash = function (req, res, buf, encoding){
const hash = crypto.createHash('sha1')
hash.setEncoding('hex')
hash.write(buf.toString(encoding || 'utf8'))
hash.end()