koel/app/Repositories/AbstractRepository.php

52 lines
1.2 KiB
PHP
Raw Normal View History

2018-08-29 06:15:11 +00:00
<?php
namespace App\Repositories;
use Exception;
2018-08-29 06:15:11 +00:00
use Illuminate\Contracts\Auth\Guard;
2019-06-16 21:12:56 +00:00
use Illuminate\Database\Eloquent\Collection;
2018-08-29 06:15:11 +00:00
use Illuminate\Database\Eloquent\Model;
abstract class AbstractRepository implements RepositoryInterface
{
/** @var Model */
protected $model;
2019-01-01 11:53:34 +00:00
/** @var Guard */
2018-08-29 06:15:11 +00:00
protected $auth;
abstract public function getModelClass(): string;
public function __construct()
2018-08-29 06:15:11 +00:00
{
$this->model = app($this->getModelClass());
// This instantiation may fail during a console command if e.g. APP_KEY is empty,
// rendering the whole installation failing.
try {
$this->auth = app(Guard::class);
2019-01-01 11:53:34 +00:00
} catch (Exception $e) {
}
2018-08-29 06:15:11 +00:00
}
public function getOneById($id): ?Model
{
return $this->model->find($id);
}
public function getByIds(array $ids): Collection
{
return $this->model->whereIn($this->model->getKeyName(), $ids)->get();
}
public function getAll(): Collection
{
return $this->model->all();
}
2020-09-06 18:21:39 +00:00
public function getFirstWhere(...$params): Model
{
return $this->model->where(...$params)->first();
}
2018-08-29 06:15:11 +00:00
}