zsikta/laravel-random-model

Laravel package for get a random model from database.

Installs: 30

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 0

Open Issues: 0

pkg:composer/zsikta/laravel-random-model

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

This package is not auto-updated.

Last update: 2025-10-17 23:37:34 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