Skip to content

Instantly share code, notes, and snippets.

View amochohan's full-sized avatar

Amo Chohan amochohan

View GitHub Profile
@amochohan
amochohan / 01_Laravel 5 Simple ACL manager_Readme.md
Last active April 22, 2024 17:19
Laravel 5 Simple ACL - Protect routes by an account / role type

#Laravel 5 Simple ACL manager

Protect your routes with user roles. Simply add a 'role_id' to the User model, install the roles table and seed if you need some example roles to get going.

If the user has a 'Root' role, then they can perform any actions.

Installation

Simply copy the files across into the appropriate directories, and register the middleware in App\Http\Kernel.php

@amochohan
amochohan / array_merge_sum.php
Created June 5, 2015 15:50
PHP merge two arrays and sum the total of a common indexes
<?php
$balanceA=array(
"USD" => 10000,
"GBP" => 20000
);
$balanceB=array(
"USD" => 10000,
"GBP" => 20000,
"JPY" => 100
@amochohan
amochohan / yahoo-finance-chart-options.md
Created February 3, 2017 15:50
Yahoo finance chart options
<?php
class CalculatorTest extends TestCase
{
/** @test */
public function it_adds_two_numbers()
{
$calculator = new Calculator();
$total = $calculator->sum(1, 2);
$this->assertSame(3, $total);
@amochohan
amochohan / example.php
Created October 27, 2017 08:09
Generating a timeseries - Carbon vs native PHP
<?php
$timeSeries = [];
// Using Carbon
$start = \Carbon\Carbon::create(1983, 9, 1, 0, 0, 0);
$end = \Carbon\Carbon::create(2050, 1, 1, 0, 0,0);
while ($start->format('Y-m-d') < $end->format('Y-m-d')) {
$start->addDay();
@amochohan
amochohan / pick.php
Created April 25, 2017 10:39
Performing a partial update
<?php
MyController extends Controller
{
public function store(Request $request)
{
$interaction = Interaction::create($this->pick((new Interaction)->getFillable(), $request);
}
/**
@amochohan
amochohan / retry.php
Created April 11, 2017 13:18
Retrying a task
<?php
try($attempts = 1, $wait = 0) {
// Try $attempts times to do something, waiting $wait milliseconds between trys
// Within the try block scope, we have access to the $attempts and $wait arguments
$this->log('Doing something important attempt: ' . $attempt);
}
catch (Exception $e) {
// Do something
}
@amochohan
amochohan / ordering-eloquent-results.php
Created March 6, 2017 12:03
Ordering Eloquent results
<?php
// Instead of using the following code
$builder = User::with('somerelationship');
if ($request->has('order_by')) {
$builder->orderBy($request->input('order_by'));
}
$users = $builder->get();
@amochohan
amochohan / example.php
Created March 6, 2017 09:53
Model factories - making relationships
<?php
$factory->define(App\User::class, function (Faker\Generator $faker) {
static $password;
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
@amochohan
amochohan / .php_cs.dist.php
Created February 21, 2017 16:43
PHP code style rules
<?php
return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => ['syntax' => 'short'],
'combine_consecutive_unsets' => true,