Skip to content

A collection of useful traits for your Laravel application.

License

Notifications You must be signed in to change notification settings

muath-ye/Laratraits

 
 

Repository files navigation

Paul Felberbauer - Unsplash #tM16SjCYy84

Latest Version on Packagist License Coverage Status

Laratraits

Laratraits is a Laravel package containing useful traits and some classes to use along your Models, Controllers, Service Providers and what not. Take a look!

Requirements

  • Laravel 7.
  • PHP 7.2.15 or later.

Installation

Fire up Composer and that's it.

composer require darkghosthunter/laratraits

This package doesn't use any Service Provider.

Usage

Just check any of these traits. Each one and other classes contains a brief explanation on how to use in the first lines.

If you want to use one, just do it.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DarkGhostHunter\Laratraits\SavesToCache;
use DarkGhostHunter\Laratraits\Eloquent\UsesUuid;

class Post extends Model
{
    use UsesUuid;
    use SavesToCache;
    
    // ...
}

What it includes

Before installing, take a look into the list. If you're only using one, just copy and paste it in your project, no problem, as each trait and file includes a copy of the MIT License.

Just remember to change the namespace if you're copy-pasting them!

Traits for everything

  • ConditionCalls: Allows an object instance to execute when and unless logic.
  • EnumerableStates: Allows a class instance to have a single allowed state.
  • FiresItself: Allows an Event to be fired conveniently.
  • Multitaps: Makes all class methods chainable, like using tap() but forever. You can exit the tap using ->target or a method name appended with AndUntap.
  • PipesThrough: Allows a class to be piped through a pipeline immediately or to a queue.
  • RendersFromMarkdown: Takes a given class property to parse Markdown text and return HTML. Compatible with Htmlable interface.
  • SavesToCache: Saves the object (or part of it) to the cache.
  • SavesToSession: Saves the object (or part of it) to the session.
  • SavesToStorage: Saves the object (or part of it) to the storage.
  • SendsToHttp: Sends the object (or part of it) through an HTTP Request.
  • ThrottleMethods: Throttles a given method in a class transparently.
  • ValidatesItself: Validates an incoming data using self-contained rules.

Useful classes

Models

  • ConditionFill: Fills an attribute if a given condition is truthy or falsy.
  • DefaultColumns: Adds a DefaultColumns Global Scope to the Model selecting only given default columns, unless overrun manually in the query.
  • EncryptsJson: Encrypts and decrypts the JSON representation of a Model.
  • FillsAttributes: Automatically fills the Model with values by each method name, like fillFooAttribute().
  • HasSlug: Allows a Model to be bound to routes using the slug like this-is-the-model. Must use an exclusive slug column in the model table.
  • ModelType: Useful for Models that share a single table but have different "types", like Publications: Article, Post, Note, etc.
  • NeighbourRecords: Allows to easily get a complete "next" and "previous" record from a given model, without using pagination.
  • UsesUuid: Automatically fills the UUID on the Model. Comes with an optional Eloquent Query Builder local scopes. You can override the UUID generation.

Casts

Global Scopes

  • MacrosEloquent: Automatically adds selective Macros to the Eloquent Builder instance itself, instead of globally, when using a Global Scope. Append macro to a public static method and that's it, done.

Middleware

Blade

Missing a trait?

You can make an issue with your proposal. Consider the logic must be contained inside a trait, or use an auxiliar class to avoid polluting the class with multiple methods. PRs are preferred with tests.

License

This package is open-sourced software licensed under the MIT license.

Laravel is a Trademark of Taylor Otwell. Copyright © 2011-2020 Laravel LLC.

About

A collection of useful traits for your Laravel application.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%