koel/app/Repositories/AbstractRepository.php

47 lines
1 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;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
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();
}
}