161 lines
3.9 KiB
PHP
161 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Traits\Model;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Str;
|
|
|
|
trait Basic
|
|
{
|
|
public static function toggle($id, $field = 'active')
|
|
{
|
|
return self::update([$field => ! self::getField($id, $field)], $id);
|
|
}
|
|
|
|
public static function getIDs()
|
|
{
|
|
return self::getAll()->pluck('id');
|
|
}
|
|
|
|
public static function getOptions($field = 'name')
|
|
{
|
|
$data = self::getModel()->pluck($field, 'id')->toArray();
|
|
asort($data, SORT_NATURAL | SORT_FLAG_CASE);
|
|
|
|
return $data;
|
|
}
|
|
|
|
public static function getName($id, $len = false)
|
|
{
|
|
$name = self::getField($id, 'name');
|
|
|
|
return $len ? Str::limit($name, $len) : $name;
|
|
}
|
|
|
|
public static function getUUID($id)
|
|
{
|
|
return self::getField($id, 'uuid');
|
|
}
|
|
|
|
public static function getByUUID($uuid, $relations = [])
|
|
{
|
|
return self::getByField('uuid', $uuid, $relations)->first();
|
|
}
|
|
|
|
public static function getIdByUUID($uuid)
|
|
{
|
|
$model = self::getByUUID($uuid);
|
|
|
|
return $model ? $model->id : false;
|
|
}
|
|
|
|
public static function generateToken()
|
|
{
|
|
return Hash::make(self::generateUUID());
|
|
}
|
|
|
|
public static function generateUUID()
|
|
{
|
|
return Str::uuid()->toString();
|
|
}
|
|
|
|
public static function getFields($field)
|
|
{
|
|
return self::getAll()->pluck($field);
|
|
}
|
|
|
|
public static function getByField($field, $value, $relations = [])
|
|
{
|
|
return self::getModel()->with($relations)->where($field, $value)->get();
|
|
}
|
|
|
|
public static function getField($id, $field)
|
|
{
|
|
$model = self::get($id);
|
|
|
|
return $model ? $model->$field : false;
|
|
}
|
|
|
|
public static function edit($id)
|
|
{
|
|
return self::get($id)->toArray();
|
|
}
|
|
|
|
public static function store($data)
|
|
{
|
|
return $data['id'] ?? false ? self::update($data) : self::create($data);
|
|
}
|
|
|
|
public static function create($data)
|
|
{
|
|
return self::getModel()->create($data);
|
|
}
|
|
|
|
public static function update($data, $id = false)
|
|
{
|
|
$id = $id ? $id : $data['id'];
|
|
$model = self::get($id);
|
|
$model->update($data);
|
|
|
|
return $model;
|
|
}
|
|
|
|
public static function destroy($id)
|
|
{
|
|
$model = self::get($id);
|
|
|
|
return $model ? $model->delete() : false;
|
|
}
|
|
|
|
public static function count()
|
|
{
|
|
return self::getModel()->count();
|
|
}
|
|
|
|
public static function firstOrCreate($search, $data = false)
|
|
{
|
|
return self::getModel()->firstOrCreate($search, $data ? $data : $search);
|
|
}
|
|
|
|
public static function deleteByField($field, $value)
|
|
{
|
|
$model = self::getModel()->where($field, $value);
|
|
|
|
return $model ? $model->delete() : false;
|
|
}
|
|
|
|
public static function get($id, $relations = false, $relationscount = false)
|
|
{
|
|
return self::getModelRelations($relations, $relationscount)->find($id);
|
|
}
|
|
|
|
public static function getAll($relations = false, $relationscount = false)
|
|
{
|
|
return self::getModelRelations($relations, $relationscount)->get();
|
|
}
|
|
|
|
public static function getModelRelations($relations = false, $relationscount = false)
|
|
{
|
|
$model = $relations ? self::getModelWithRelations($relations) : false;
|
|
$model = $relationscount ? self::getModelWithCountRelations($relationscount, $model) : $model;
|
|
|
|
return $model ? $model : self::getModel();
|
|
}
|
|
|
|
public static function getModelWithRelations($relations = false, $model = false)
|
|
{
|
|
return is_object($model) ? $model->with($relations) : self::getModel()->with($relations);
|
|
}
|
|
|
|
public static function getModelWithCountRelations($relations = false, $model = false)
|
|
{
|
|
return is_object($model) ? $model->withCount($relations) : self::getModel()->withCount($relations);
|
|
}
|
|
|
|
public static function getModel(): Model
|
|
{
|
|
return new Model();
|
|
}
|
|
}
|