diff --git a/kernel/RestController.php b/kernel/RestController.php index e13d436..5bb63b6 100644 --- a/kernel/RestController.php +++ b/kernel/RestController.php @@ -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,11 @@ class RestController #[NoReturn] public function actionView($id): void { + $expand = $this->expand(); $model = $this->model->where("id", $id)->first(); + if ($expand){ + $model->load($expand); + } $res = []; if ($model){ $res = $model->toArray(); diff --git a/kernel/modules/post/controllers/PostRestController.php b/kernel/modules/post/controllers/PostRestController.php index 6552daf..c640608 100644 --- a/kernel/modules/post/controllers/PostRestController.php +++ b/kernel/modules/post/controllers/PostRestController.php @@ -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(); - } - - $this->renderApi($res); + return ["user"]; } - #[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); - - } } \ No newline at end of file diff --git a/kernel/modules/post/models/Post.php b/kernel/modules/post/models/Post.php index b3482d1..aa58ca2 100644 --- a/kernel/modules/post/models/Post.php +++ b/kernel/modules/post/models/Post.php @@ -9,7 +9,6 @@ use kernel\modules\user\models\User; * @property string $content * @property string $title * @property string $slug -// * @property string $username * @property int $user_id * @method static where(int[] $array) * @method static find($id) @@ -31,7 +30,7 @@ class Post extends Model ]; } - public function user() + public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(User::class); }