rukhsar/addontraits

Collection of user full addon traits to perform random model and orderby functionality.

v1.1 2016-11-30 12:00 UTC

This package is not auto-updated.

Last update: 2024-11-10 02:09:39 UTC


README

Collection of useful Laravel model traits to retrieve random model and add OrderBy functionality.

Installation

Require this package by running:

composer require rukhsar/addontraits

Laravel OrderByTrait

Usage

Add support for a standard order by

use Rukhsar\AddonTraits\OrderByTrait;

class User extends Model
{
    use OrderByTrait;

    protected $default_order_by = 'name';

    protected $default_order_direction = 'asc';

}

Use Order() in your query to fetch the result.

    $users = App\User::Order()->get();

You can select any column to add OrderBy functionality either in asc or desc order.

Laravel RandomModelTrait

Laravel package for getting a random model from database.

Usage

Use RandomModelTrait in your model.

use Rukhsar\AddonTraits\RandomModelTrait;

class User extends Model
{
    use RandomModelTrait;
}

Now you can use random() scope on your model.

$randomUser = User::random()->first();

$randomUserWithRelation = User::random()->with('relation_name')->first();

// Static methods which return an instance

$user = User::getRandom();  // return instance or null

$user = User::getRandomOrFail(); // return instance or throw exception