Compare commits
3 Commits
a26eeb9441
...
c7e113e325
Author | SHA1 | Date | |
---|---|---|---|
c7e113e325 | |||
04da662275 | |||
5fe6bd9629 |
@ -119,11 +119,11 @@ class Request
|
||||
|
||||
/**
|
||||
* Возвращает GET - параметр.
|
||||
* @param string $param Параметр.
|
||||
* @param mixed $defaultValue Значение если, параметр не передан.
|
||||
* @param string|null $param Параметр.
|
||||
* @param mixed|null $defaultValue Значение если, параметр не передан.
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($param = null, $defaultValue = null): mixed
|
||||
public function get(string $param = null, mixed $defaultValue = null): mixed
|
||||
{
|
||||
if (is_null($param)) {
|
||||
return $_GET;
|
||||
|
@ -11,6 +11,12 @@ class RestController
|
||||
|
||||
protected Model $model;
|
||||
|
||||
|
||||
protected function expand(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionIndex(): void
|
||||
{
|
||||
$request = new Request();
|
||||
@ -29,7 +35,14 @@ class RestController
|
||||
|
||||
#[NoReturn] public function actionView($id): void
|
||||
{
|
||||
$expand = $this->expand();
|
||||
$request = new Request();
|
||||
$expandParams = explode( ",", $request->get('expand') ?? "");
|
||||
$model = $this->model->where("id", $id)->first();
|
||||
$finalExpand = array_intersect($expandParams, $expand);
|
||||
if ($finalExpand){
|
||||
$model->load($finalExpand);
|
||||
}
|
||||
$res = [];
|
||||
if ($model){
|
||||
$res = $model->toArray();
|
||||
|
@ -14,30 +14,9 @@ class PostRestController extends RestController
|
||||
$this->model = new Post();
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionView($id): void
|
||||
protected function expand(): array
|
||||
{
|
||||
$model = $this->model->where("id", $id)->first();
|
||||
$model->load("user");
|
||||
$res = [];
|
||||
if ($model){
|
||||
$res = $model->toArray();
|
||||
return ["user"];
|
||||
}
|
||||
|
||||
$this->renderApi($res);
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionDelete($id): void
|
||||
{
|
||||
$model = $this->model->where("id", $id)->first();
|
||||
$model->load("user");
|
||||
$res = [];
|
||||
if ($model){
|
||||
$res = $model->toArray();
|
||||
}
|
||||
|
||||
$model->delete();
|
||||
|
||||
$this->renderApi($res);
|
||||
|
||||
}
|
||||
}
|
@ -30,7 +30,7 @@ class Post extends Model
|
||||
];
|
||||
}
|
||||
|
||||
public function user()
|
||||
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
Reference in New Issue
Block a user