128 lines
3.1 KiB
PHP
128 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Traits\Model;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
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)
|
|
{
|
|
return self::getField($id, 'name');
|
|
}
|
|
|
|
public static function getByUUID($uuid)
|
|
{
|
|
return self::getByField('uuid', $uuid)->first();
|
|
}
|
|
|
|
public static function getFields($field)
|
|
{
|
|
return self::getAll()->pluck($field);
|
|
}
|
|
|
|
public static function getByField($field, $value)
|
|
{
|
|
return self::getModel()->where($field, $value);
|
|
}
|
|
|
|
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)
|
|
{
|
|
return self::getModel()::firstOrCreate($search, $data);
|
|
}
|
|
|
|
public static function get($id, $relations = false, $relations_count = false)
|
|
{
|
|
return self::getModelRelations($relations, $relations_count)->find($id);
|
|
}
|
|
|
|
public static function getAll($relations = false, $relations_count = false)
|
|
{
|
|
return self::getModelRelations($relations, $relations_count)->get();
|
|
}
|
|
|
|
public static function getModelRelations($relations = false, $relations_count = false)
|
|
{
|
|
$model = $relations ? self::getModelWithRelations($relations) : false;
|
|
$model = $relations_count ? self::getModelWithCountRelations($relations_count, $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();
|
|
}
|
|
}
|