zsikta/laravel-random-model

Laravel package for get a random model from database.
30 2
Install
composer require zsikta/laravel-random-model
Latest Version:v0.1.0
PHP:>=5.5.9
License:MIT
Last Updated:Apr 11, 2016
Links: GitHub  ·  Packagist
Maintainer: zsikta

Laravel random model

Laravel package for get a random model from database.

Installation

Require this package with composer:

composer require zsikta/laravel-random-model

Usage

Use RandomQueryable trait in your model:

<?php

use ZsikTa\LaravelRandomModel\RandomQueryable;

class YourModel extends Model
{
    use RandomQueryable;
}

You can now use as scope:

$singleModel = YourModel::random()->first();

$modelWithRelation = YourModel::random()->with('relation_name')->first();

Or static getter methods which return an instance:

$model = YourModel::getRandom(); // returns an instance or null

$otherModel = YourModel::getRandomOrFail(); // returns an instance or throws exception