add select

This commit is contained in:
Билай Станислав 2024-07-26 16:09:06 +03:00
parent 2d8f945cad
commit 53eb536213
11 changed files with 72 additions and 17 deletions

View File

@ -30,8 +30,12 @@ class PostController extends Controller
$postForm = new CreatePostForm(); $postForm = new CreatePostForm();
$postService = new PostService(); $postService = new PostService();
$postForm->load($_REQUEST); $postForm->load($_REQUEST);
// Debug::dd($_REQUEST);
if(UserService::check($_REQUEST['user_id'])) { if(UserService::check($_REQUEST['user_id'])) {
// Debug::dd(123);
if ($postForm->validate()) { if ($postForm->validate()) {
// Debug::dd($postForm);
$post = $postService->create($postForm); $post = $postService->create($postForm);
if ($post) { if ($post) {
$this->redirect("/admin/post/" . $post->id); $this->redirect("/admin/post/" . $post->id);
@ -103,7 +107,8 @@ class PostController extends Controller
#[NoReturn] public function actionDelete($id): void #[NoReturn] public function actionDelete($id): void
{ {
Post::find($id)->delete(); $post = Post::find($id)->first();
$post->delete();
$this->redirect("/admin/post/"); $this->redirect("/admin/post/");
} }
} }

View File

@ -10,7 +10,7 @@ class CreatePostForm extends FormModel
{ {
return [ return [
'content' => 'required|min-str-len:1', 'content' => 'required|min-str-len:1',
'user_id' => 'required|min-str-len:1', 'user_id' => 'required',
]; ];
} }
} }

View File

@ -44,4 +44,16 @@ class UserService
return false; return false;
} }
public static function createUsernameArr(): array
{
foreach (User::all()->toArray() as $user) {
$userArr[$user['id']] = $user['username'];;
}
if (!empty($userArr)) {
return $userArr;
}
return [];
}
} }

View File

@ -0,0 +1,16 @@
<?php
namespace app\tables\columns;
use Itguild\Tables\ActionColumn\ActionColumn;
class PostDeleteActionColumn extends ActionColumn
{
protected string $prefix = "/delete/";
public function fetch(): string
{
$link = $this->baseUrl . $this->prefix . $this->id;
return " <a href='$link' class='btn btn-danger'>Удалить</a> ";
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace app\tables\columns;
use Itguild\Tables\ActionColumn\ActionColumn;
class PostViewActionColumn extends actionColumn
{
protected string $prefix = "/";
public function fetch(): string
{
$link = $this->baseUrl . $this->prefix . $this->id;
return " <a href='$link' class='btn btn-primary'>Просмотр</a> ";
}
}

View File

@ -7,11 +7,11 @@ use Itguild\Tables\ActionColumn\ActionColumn;
class UserDeleteActionColumn extends ActionColumn class UserDeleteActionColumn extends ActionColumn
{ {
protected string $prefix = "/"; protected string $prefix = "/delete/";
public function fetch(): string public function fetch(): string
{ {
$link = $this->baseUrl . $this->prefix . "delete" . $this->prefix . $this->id; $link = $this->baseUrl . $this->prefix . $this->id;
return " <a href='$link' class='btn btn-danger'>Удалить</a> "; return " <a href='$link' class='btn btn-danger'>Удалить</a> ";
} }
} }

View File

@ -6,7 +6,7 @@ use Itguild\Tables\ActionColumn\ActionColumn;
class UserEditActionColumn extends ActionColumn class UserEditActionColumn extends ActionColumn
{ {
protected string $prefix = "/"; protected string $prefix = "/update/";
public function fetch(): string public function fetch(): string
{ {

2
m.php
View File

@ -9,6 +9,6 @@ require_once __DIR__ . "/vendor/autoload.php";
require_once __DIR__ . "/bootstrap.php"; require_once __DIR__ . "/bootstrap.php";
//UserMigration::up(); //UserMigration::up();
PostMigration::up(); //PostMigration::up();
//AnswerMigration::up(); //AnswerMigration::up();
//QuestionMigration::up(); //QuestionMigration::up();

View File

@ -18,7 +18,7 @@ class PostMigration extends Migration
{ {
Manager::schema()->create('post', function (Blueprint $table) { Manager::schema()->create('post', function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->string('content', 3000)->nullable(false); $table->text('content')->nullable(false);
$table->integer('user_id'); $table->integer('user_id');
$table->timestamps(); $table->timestamps();
}); });

View File

@ -4,6 +4,7 @@
*/ */
use app\models\Post; use app\models\Post;
use app\models\User;
$form = new \itguild\forms\ActiveForm(); $form = new \itguild\forms\ActiveForm();
$form->beginForm(isset($model) ? "/admin/post/edit/" . $model->id : "/admin/post"); $form->beginForm(isset($model) ? "/admin/post/edit/" . $model->id : "/admin/post");
@ -16,14 +17,19 @@ $form->field(class: \itguild\forms\inputs\TextArea::class, name: "content", para
]) ])
->setLabel("Пост") ->setLabel("Пост")
->render(); ->render();
$form->field(class: \itguild\forms\inputs\Select::class, name: "user_id", params: [
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "user_id", params: [ //$form->field(class: \itguild\forms\inputs\Select::class, name: "username", params: [
'class' => "form-control", 'class' => "form-control",
'placeholder' => 'id',
'value' => $model->user_id ?? '' 'value' => $model->user_id ?? ''
]) ])
->setLabel("id пользователя") ->setLabel("Пользователи")
->setOptions(\app\services\UserService::createUsernameArr())
// ->setOptions([\app\services\UserService::createIdArr()[74] => \app\services\UserService::createUsernameArr()[1],
// \app\services\UserService::createUsernameArr()[74],
// \app\services\UserService::createUsernameArr()[82],])
->render(); ->render();
\app\helpers\Debug::prn(\app\services\UserService::createUsernameArr());
?> ?>
<div class="row"> <div class="row">
<div class="col-sm-2"> <div class="col-sm-2">

View File

@ -5,9 +5,9 @@
*/ */
use app\models\Post; use app\models\Post;
use app\tables\columns\UserDeleteActionColumn; use app\tables\columns\PostDeleteActionColumn;
use app\tables\columns\UserEditActionColumn; use app\tables\columns\PostEditActionColumn;
use app\tables\columns\UserViewActionColumn; use app\tables\columns\PostViewActionColumn;
use Itguild\Tables\ListJsonTable; use Itguild\Tables\ListJsonTable;
use kernel\IGTabel\btn\PrimaryBtn; use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\ListJsonTableEloquentCollection; use kernel\IGTabel\ListJsonTableEloquentCollection;
@ -24,8 +24,8 @@ $table->beforePrint(function (){
return PrimaryBtn::create("Создать", "/admin/post/create")->fetch(); return PrimaryBtn::create("Создать", "/admin/post/create")->fetch();
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch(); //return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
}); });
$table->addAction(UserViewActionColumn::class); $table->addAction(PostViewActionColumn::class);
$table->addAction(\app\tables\columns\PostEditActionColumn::class); $table->addAction(PostEditActionColumn::class);
$table->addAction(UserDeleteActionColumn::class); $table->addAction(PostDeleteActionColumn::class);
$table->create(); $table->create();
$table->render(); $table->render();