52 lines
1 KiB
PHP
52 lines
1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Word extends Model
|
|
{
|
|
/**
|
|
* Indicates if the model should be timestamped.
|
|
*
|
|
* @var bool
|
|
*/
|
|
public $timestamps = false;
|
|
|
|
/**
|
|
* Get the route key for the model.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getRouteKeyName()
|
|
{
|
|
return 'uuid';
|
|
}
|
|
|
|
static public function boot()
|
|
{
|
|
static::saving(function($model) {
|
|
$model->uuid = substr(base64_encode($model->value), 0, 10);
|
|
});
|
|
|
|
parent::boot();
|
|
}
|
|
|
|
/**
|
|
* Return a random row from the table.
|
|
*
|
|
* @var mixed $exclude uuid's of rows that should be excluded.
|
|
*/
|
|
static public function getRandom($exclude = [])
|
|
{
|
|
$q = self::inRandomOrder();
|
|
|
|
if (!is_array($exclude)) {
|
|
$exclude = [ $exclude ];
|
|
}
|
|
if (count($exclude) > 1) {
|
|
$q->where("uuid", 'NOT IN', $exclude);
|
|
}
|
|
return $q->first();
|
|
}
|
|
}
|