login
This commit is contained in:
57
app/Models/Base/BaseModel.php
Normal file
57
app/Models/Base/BaseModel.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Base;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class BaseModel extends Model
|
||||
{
|
||||
|
||||
function checkColInFill($aItem)
|
||||
{
|
||||
foreach ($aItem as $key => $value) {
|
||||
if (!in_array($key, $this->fillable)) {
|
||||
unset($aItem[$key]);
|
||||
}
|
||||
}
|
||||
return $aItem;
|
||||
}
|
||||
|
||||
function addItem($aItem): Model|\Illuminate\Database\Eloquent\Builder|bool
|
||||
{
|
||||
$aItem = $this->checkColInFill($aItem);
|
||||
if (empty($aItem)) return false;
|
||||
return $this->newQuery()->create($aItem);
|
||||
}
|
||||
|
||||
function delItem($id)
|
||||
{
|
||||
return $this->newQuery()->where($this->primaryKey, $id)->delete();
|
||||
}
|
||||
|
||||
function updateItem($aItem): bool|int
|
||||
{
|
||||
$aItem = $this->checkColInFill($aItem);
|
||||
if (empty($aItem)) return false;
|
||||
if (isset($aItem[$this->primaryKey])) return false;
|
||||
return $this->newQuery()->where($this->primaryKey,$aItem[$this->primaryKey])->update($aItem);
|
||||
}
|
||||
|
||||
function findItem($id,$col=['*']): Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|array|null
|
||||
{
|
||||
return $this->newQuery()->find($id,$col);
|
||||
}
|
||||
|
||||
function findItemByWhere($aWhere,$col=['*']): Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|array|null
|
||||
{
|
||||
return $this->newQuery()->where($aWhere)->first($col);
|
||||
}
|
||||
|
||||
function getItemsByWhere($aWhere,$col=['*']): \Illuminate\Database\Eloquent\Collection|array
|
||||
{
|
||||
return $this->newQuery()->where($aWhere)->get($col);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user