• This repository has been archived on 04/Mar/2024
  • Stars
    star
    144
  • Rank 247,612 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A collection of useful traits for your Laravel application.

Paul Felberbauer - Unsplash #tM16SjCYy84

Latest Version on Packagist License Coverage Status Laravel Octane Compatible

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 8.0.
  • PHP 7.4, PHP 8.0 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

  • Comparable: Allows a class or its properties to be compared to a list of values.
  • 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.
  • RegeneratesCache: Stores a copy of an object (or part of it) avoiding data-races among other processes.
  • 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.
  • SecurelyJsonable: Adds a signature to the Jsonable object that is checked to at unserialization to avoid tampering.
  • SendsToHttp: Sends the object (or part of it) through an HTTP Request.
  • ShadowCall: Allows the next method to be called depending on a condition.
  • ThrottleMethods: Throttles a given method in a class transparently.
  • Throws: Allows a Throwable to throw itself if a given condition is truthy or falsy.
  • 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.
  • FillsAttributes: Automatically fills the Model with values by each method name, like fillFooAttribute().
  • FromRequest: One-liners to make, create, or update models from the HTTP Request.
  • 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

  • CastEnumerable: Allows an Enumerable to be saved as an integer o string, and retrieved back to an Enumerable instance.
  • CastsRepository: Allows a Config Repository string to be saved a JSON, and retrieved back as Config Repository.
  • CastsBase64: Allows a binary value to be saved as BASE64 in the database, and retrieved back as binary.

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

Migrations:

  • PublishesMigrations: Allows a package to register migrations automatically as publishable assets.

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.

More Repositories

1

Larapass

Authenticate users with just their device, fingerprint or biometric data. Goodbye passwords!
PHP
580
star
2

Preloader

Preloader helper to create a PHP-ready preload script from Opcache.
PHP
428
star
3

Laraguard

"On-premises 2FA Authentication for all your users out-of-the-box
PHP
267
star
4

Laraload

Effortlessly create a PHP preload script for your Laravel project.
PHP
210
star
5

Laraconfig

Per-user settings repository system for Laravel
PHP
173
star
6

Captchavel

Integrate reCAPTCHA into your Laravel app better than the Big G itself!
PHP
96
star
7

RememberableQuery

Remember your Query results using only one method. Yes, only one.
PHP
49
star
8

Larahelp

Supercharge your Laravel projects with more than 25 useful global helpers.
PHP
49
star
9

Passless

Passwordless Authentication Driver for Laravel. Just add water.
PHP
45
star
10

Laralerts

Quickly create one or multiple Alerts from your backend.
PHP
45
star
11

Laralocker

Avoid race conditions in your Jobs, Listeners and Notifications with this simple locker reservation system.
PHP
44
star
12

Larapoke

Keep your forms alive, avoid TokenMismatchException by gently poking your Laravel app.
PHP
43
star
13

Larasane

Quickly and easily secure HTML text.
PHP
39
star
14

Tailstart

HTML Components ready to style with Tailwind CSS
HTML
8
star
15

FlowSdk

Flow SDK for PHP to process payments, made unofficially.
PHP
5
star
16

Laraflow

Flow SDK for Laravel
PHP
4
star
17

RutUtils

A complete library for creating, manipulating and generating chilean RUTs or RUNs.
PHP
3
star
18

Laradate

Parse a date from the URL, receive it as a `Carbon` instance in your controller.
PHP
2
star
19

Fluid

A flexible class based on the famous Laravel's Fluent and Eloquent Model class.
PHP
2
star
20

Larabanker

This package connects the non-official Transbank SDK into your Laravel Application.
PHP
2
star
21

TransbankApi

Transbank SDK no oficial para PHP 8.0
PHP
2
star
22

Transbanker

Transbank connector for Laravel
PHP
1
star
23

Lararut

RutUtils integration for Laravel.
PHP
1
star