Rukhsar / laravel-addon-traits by Rukhsar

Collection of user full addon traits to perform random model and orderby functionality.
7
0
2
Package Data
Maintainer Username: Rukhsar
Maintainer Contact: rukhsar.man@gmail.com (Rukhsar Manzoor)
Package Create Date: 2016-11-30
Package Last Update: 2016-11-30
Language: PHP
License: MIT
Last Refreshed: 2024-11-17 03:01:36
Package Statistics
Total Downloads: 7
Monthly Downloads: 1
Daily Downloads: 0
Total Stars: 0
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Laravel Addon Traits

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