数据管理
建议编辑本指南提供了一个用于创建和编辑模型的最小集合。在当前示例中,我们将为博客创建管理页面。
注意。 本教程是教程的重要部分,但如果您的应用程序需要简单的 CRUD,请考虑使用专用包。
在这个阶段,建议您已经熟悉屏幕的基本概念,并观看“初学者快速入门”。
首先,我们需要创建一个新表。为此,执行以下命令:
php artisan make:migration create_posts_table
将在 database/migrations
目录中创建一个新的迁移文件,我们将向其中添加所需列的描述。
// `****_**_**_create_posts_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* 运行迁移。
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('description');
$table->text('body');
$table->bigInteger('author');
$table->timestamps();
});
}
/**
* 回滚迁移。
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
要将新方案应用于连接的数据库,我们将执行:
php artisan migrate
成功创建表后,添加一个新的 Eloquent
模型,为此:
php artisan make:model Post
在 app
目录中,将创建一个新文件 Post.php
,我们将描述可填充的字段:
// app/Models/Post.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Orchid\Screen\AsSource;
class Post extends Model
{
use AsSource;
/**
* @var array
*/
protected $fillable = [
'title',
'description',
'body',
'author'
];
}
注意。 模型具有
AsSource
trait,以便通过点符号方便地处理。
现在我们准备好实际使用平台。
在上一教程中,我们已经创建了用于显示帖子第一个屏幕,但现在我们需要同时显示记录和编辑它们,因此我们将为每个操作添加两个新屏幕,依次执行命令:
php artisan orchid:screen PostEditScreen
php artisan orchid:screen PostListScreen
注意: 创建两个或多个屏幕以提供 CRUD 是可选的,但为了方便使用。
在应用程序路由列表中注册新屏幕:
// routes/platform.php
use App\Orchid\Screens\PostEditScreen;
use App\Orchid\Screens\PostListScreen;
Route::screen('post/{post?}', PostEditScreen::class)
->name('platform.post.edit');
Route::screen('posts', PostListScreen::class)
->name('platform.post.list');
现在可以在地址 /admin/post
和 /admin/posts
查看屏幕。
当前生成的屏幕没有数据,没有操作,编辑 PostEditScreen
以添加名称、描述和所需字段:
namespace App\Orchid\Screens;
use App\Models\Post;
use App\Models\User;
use Illuminate\Http\Request;
use Orchid\Screen\Fields\Input;
use Orchid\Screen\Fields\Quill;
use Orchid\Screen\Fields\Relation;
use Orchid\Screen\Fields\TextArea;
use Orchid\Screen\Fields\Upload;
use Orchid\Support\Facades\Layout;
use Orchid\Screen\Actions\Button;
use Orchid\Screen\Screen;
use Orchid\Support\Facades\Alert;
class PostEditScreen extends Screen
{
/**
* @var Post
*/
public $post;
/**
* 查询数据。
*
* @param Post $post
*
* @return array
*/
public function query(Post $post): array
{
return [
'post' => $post
];
}
/**
* 名称显示在用户屏幕和标题中
*/
public function name(): ?string
{
return $this->post->exists ? '编辑帖子' : '创建新帖子';
}
/**
* 描述显示在用户屏幕的标题下
*/
public function description(): ?string
{
return "博客帖子";
}
/**
* 按钮命令。
*
* @return Link[]
*/
public function commandBar(): array
{
return [
Button::make('创建帖子')
->icon('pencil')
->method('createOrUpdate')
->canSee(!$this->post->exists),
Button::make('更新')
->icon('note')
->method('createOrUpdate')
->canSee($this->post->exists),
Button::make('删除')
->icon('trash')
->method('remove')
->canSee($this->post->exists),
];
}
/**
* 视图。
*
* @return Layout[]
*/
public function layout(): array
{
return [
Layout::rows([
Input::make('post.title')
->title('标题')
->placeholder('吸引但神秘的标题')
->help('为此帖子指定一个简短的描述性标题。'),
TextArea::make('post.description')
->title('描述')
->rows(3)
->maxlength(200)
->placeholder('预览的简短描述'),
Relation::make('post.author')
->title('作者')
->fromModel(User::class, 'name'),
Quill::make('post.body')
->title('主要文本'),
])
];
}
/**
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function createOrUpdate(Request $request)
{
$this->post->fill($request->get('post'))->save();
Alert::info('您已成功创建帖子。');
return redirect()->route('platform.post.list');
}
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function remove()
{
$this->post->delete();
Alert::info('您已成功删除帖子。');
return redirect()->route('platform.post.list');
}
}
现在我们可以创建、编辑和删除记录。但不能以列表形式浏览。改变它!
在所有以前的层定义中,仅使用了 Layout:rows()
的简短记录,但为了显示复杂或大量的信息,建议创建单独的类。
通过运行命令添加一个新的 Layout
类:
php artisan orchid:table PostListLayout
我们指出希望看到的数据:
namespace App\Orchid\Layouts;
use App\Models\Post;
use Orchid\Screen\TD;
use Orchid\Screen\Actions\Link;
use Orchid\Screen\Layouts\Table;
class PostListLayout extends Table
{
/**
* 数据源。
*
* @var string
*/
public $target = 'posts';
/**
* @return TD[]
*/
public function columns(): array
{
return [
TD::make('title', '标题')
->render(function (Post $post) {
return Link::make($post->title)
->route('platform.post.edit', $post);
}),
TD::make('created_at', '创建时间'),
TD::make('updated_at', '最后编辑'),
];
}
}
target
属性指示哪个键将是我们屏幕上的表的来源。
注意。 直接在类中生成
HTML
仅是一个示例,并且是不好的做法。请使用Blade
模板。
定义表层后,我们返回到视图屏幕,进行更改:
namespace App\Orchid\Screens;
use App\Orchid\Layouts\PostListLayout;
use App\Models\Post;
use Orchid\Screen\Actions\Link;
use Orchid\Screen\Screen;
class PostListScreen extends Screen
{
/**
* 查询数据。
*
* @return array
*/
public function query(): array
{
return [
'posts' => Post::paginate()
];
}
/**
* 名称显示在用户屏幕和标题中
*/
public function name(): ?string
{
return '博客帖子';
}
/**
* 描述显示在用户屏幕的标题下
*/
public function description(): ?string
{
return "所有博客帖子";
}
/**
* 按钮命令。
*
* @return Link[]
*/
public function commandBar(): array
{
return [
Link::make('创建新')
->icon('pencil')
->route('platform.post.edit')
];
}
/**
* 视图。
*
* @return Layout[]
*/
public function layout(): array
{
return [
PostListLayout::class
];
}
}
现在我们有一个用于管理(创建、查看、编辑和删除条目)博客的集合,不要忘记通过添加菜单项和面包屑来确保导航的便利性。