Skip to content

Instantly share code, notes, and snippets.

@martinandersen3d
martinandersen3d / FileTrait.php
Created March 21, 2020 13:27
Download File and get FileName methods Laravel Framework
<?php
namespace App\Traits;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
trait FileTrait
{
@martinandersen3d
martinandersen3d / ConvertsNumbers.php
Created March 21, 2020 13:22 — forked from andyunleashed/ConvertsNumbers.php
A trait for converting numbers to and from integers/floats for display and storage.
<?php
namespace App\Traits;
trait ConvertsNumbers {
public function isConvertable($key)
{
if(!isset($this->convertable)) {
return false;
<?php
/**
* usage in a controlller:
* public function index($page=1) {
* $users = User::paginateUri(5, $page);
* return view('users', compact('users'));
* }
* it also support variable number of parameters,
* routes must be defined using {page} or {page?} placeholders
* $links is returned as reference and contain a standard bootstrap 3 navigation
@martinandersen3d
martinandersen3d / RestControllerTrait.php
Created March 21, 2020 12:58 — forked from beanmoss/RestControllerTrait.php
Playing with Laravel Lumen: simple RESTful trait.
<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;
trait RestControllerTrait
{
public function index()
{
$m = self::MODEL;
return $this->listResponse($m::all());
@martinandersen3d
martinandersen3d / CastsValueObject.php
Created March 21, 2020 12:56 — forked from cmaas/CastsValueObject.php
A Trait to automatically cast value objects in Laravel without needing a Mutator and an Accessor.
<?php
trait CastsValueObjects
{
protected function castAttribute($key, $value)
{
$castToClass = $this->getValueObjectCastType($key);
// no Value Object? simply pass this up to the parent
if (!$castToClass) {
return parent::castAttribute($key, $value);
@martinandersen3d
martinandersen3d / Enums.php
Created March 21, 2020 12:54 — forked from jhoff/Enums.php
Laravel Model Enumeration Trait
<?php
namespace App\Traits;
use Illuminate\Support\Str;
use App\Exceptions\InvalidEnumException;
trait Enums
{
/**
@martinandersen3d
martinandersen3d / 1_Laravel_state-machine.md
Created March 21, 2020 12:25 — forked from iben12/1_Laravel_state-machine.md
Laravel: State-machine on Eloquent Model

Implementing State Machine On Eloquent Model*

* Update (12.09.2017): I have improved the trait so that it can be used with objects other than Eloquent Models.

Some days ago I came across a task where I needed to implement managable state for an Eloquent model. This is a common task, actually there is a mathematical model called "Finite-state Machine". The concept is that the state machine (SM) "can be in exactly one of the finite number of states at any given time". Also changing from one state to another (called transition) depends on fulfilling the conditions defined by its configuration.

Practically this means you define each state that the SM can be in and the possible transitions. To define a transition you set the states on which the transition can be applied (initial conditions) and the only state in which the SM should be after the transition.

That's the theory, let's get to the work.

@martinandersen3d
martinandersen3d / README.md
Created March 21, 2020 12:05 — forked from mul14/README.md
Simple Laravel Search Trait

Usage

Put SearchTrait.php in app directory. Then use SearchTrait in your model, like so

use App\SearchTrait;
use Illuminate\Database\Eloquent\Model;

class Article extends Model {
@martinandersen3d
martinandersen3d / OutFormatTrait.md
Created March 21, 2020 12:01 — forked from ifreesec/OutFormatTrait.md
简单封装了下 php 开发 API json化快捷输出结果的 Trait

以下是 laravel 版本,增加了 headers

OutFormatTrait.php

<?php
/**
 * 格式化输出数据
 *
 * Class outFormat
 * @package App\Sanjieke\Logic