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