zsikta/laravel-random-model

Laravel package for get a random model from database.

v0.1.0 2016-04-11 20:27 UTC

This package is not auto-updated.

Last update: 2024-05-03 16:47:49 UTC


README

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