zsikta / laravel-random-model by zsikta

Laravel package for get a random model from database.
29
2
2
Package Data
Maintainer Username: zsikta
Maintainer Contact: zsikai.tamas@gmail.com (Tamas Zsikai)
Package Create Date: 2016-04-11
Package Last Update: 2016-04-11
Language: PHP
License: MIT
Last Refreshed: 2024-11-13 15:01:56
Package Statistics
Total Downloads: 29
Monthly Downloads: 1
Daily Downloads: 0
Total Stars: 2
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

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