$cities = require __DIR__ . "/cities.php";
../Чтобы добраться до нужной папки
$cities = require __DIR__ . "/../configs/cities.php";
...
public function store(Request $request)
{
$this->dispatchNow(
new CreateWarehouseCommand(
$request->user(),
$request->validated()
)
);
}
...
class CreateWareHouseCommand
{
private User $user;
private array $data;
public function __construct($user, $data)
{
$this->user = $user;
$this->data = $data;
}
public function handle(WarehouseService $service)
{
DB::transaction(function () use ($service) {
$service->create($this->user, $this->data);
// если надо создать что то еще можно вызывать другие сервисы
});
}
}
class WarehouseService
{
private $repository;
public function __construct(WarehouseRepository $repository)
{
$this->repository = $repository;
}
public function create(User $user, array $data): void
{
$model = $this->newModel();
$model->fill($data);
$model->author()->associate($user);
$this->repository->save($model);
}
private function newModel(): Warehouse
{
return App::make(Warehouse::class);
}
...
// update, delete, ....
}
interface RepositoryInterface
{
public function save(Model $model): void;
public function delete(Model $model): void;
}
trait RepositoryTrait
{
abstract protected function getModel(): Eloquent\Model;
private function getQuery(): Eloquent\Builder
{
return $this->getModel()->getQuery();
}
public function save(Model $model): void
{
$model->save();
}
public function delete(Model $model): void
{
$model->delete();
}
}
class Warehouse implements RepositoryInterface
{
use RepositoryTrait;
public function getByUser(User $user): Eloquent\Collection
{
return $this
->getQuery()
->leftJoin(филиалы...)
->leftJoin(пользователи...)
->where(пользователь = $user)
->get();
}
protected function getModel(): Eloquent\Model
{
return App::make(Warehouse::class);
}
}
$cities = require __DIR__ . "/config/cities.php";