laravel orchid快速上手–一个简陋的页面(四)
修改完数据model后,直接打开\app\Orchid\Screens\ItemNameEditScreen.php ,也就是路由指向的platform.item_name.edit
我已经做了详细的注释。首先先use screen 。。。扩展出页面,然后use model。。再use页面上需要用到的字段【我用的简易模式,直接在本页面构建修改页面的view】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
<?php namespace App\Orchid\Screens; use Orchid\Screen\Screen; use App\Models\BasicItem; use App\Models\ItemName; 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\Support\Facades\Alert; class ItemNameEditScreen extends Screen { //定义传入的数据 public $name_translation; /** * Query data. * * @return array */ public function query(ItemName $name_translation): iterable { // 数据来源是model ItemName 因为表有个字段是item_name,所以我把传送过来的数据改成name_translation。这个相当于整个页面的数据了。 return [ 'name_translation'=>$name_translation ]; } /** * Display header name. * * @return string|null */ public function name(): ?string { //head上显示的名字 return $this->name_translation->exists ? 'Edit Item Name Language' : 'Creating a new Item Name Language'; } /** * The description is displayed on the user's screen under the heading */ public function description(): ?string { // 页面head下的描述 return __('Item Name'); } /** * Button commands. * * @return \Orchid\Screen\Action[] */ public function commandBar(): iterable { // 页面上的增删改查按钮 return [ Button::make(__('Create Item Language')) ->icon('pencil') ->method('createOrUpdate') ->canSee(!$this->name_translation->exists), //如果数据不存在,则可以看到这个按钮。 Button::make(__('Update Item Language')) ->icon('note') ->method('createOrUpdate') ->canSee($this->name_translation->exists), Button::make(__('Remove Item Language')) ->icon('trash') ->method('remove') ->canSee($this->name_translation->exists), ]; } /** * Views. * * @return \Orchid\Screen\Layout[]|string[] */ public function layout(): iterable { //视图层 可以新增一个类来填充 这里试试用简单的方式展示出来,由于我这个数据表是不用改动的,所以后面会换表演示 return [ Layout::rows([ //这个注意,是一个关系表。 Relation::make('name_translation.item_id') ->title('item_id') ->fromModel(BasicItem::class, 'UniqueName'), Input::make('name_translation.local_language') ->title('Language') ->placeholder('en') ->help('Please use the abbreviation of the language, for example: en.'), Input::make('name_translation.item_name') ->title('Item name') ->placeholder('Please enter the text translation for the item'), TextArea::make('name_translation.item_desc') ->title('Item desc') ->rows(3) ->maxlength(200) ->placeholder('Please enter a description of the project.'), ]) ]; } /** * @param \Illuminate\Http\Request $request * * @return \Illuminate\Http\RedirectResponse */ public function createOrUpdate(Request $request) { if(empty($this->name_translation)) { ItemName::create($request->get('name_translation')); } else { $this->name_translation->fill($request->get('name_translation'))->save(); } Alert::info('You have successfully created a item language.'); return redirect()->route('platform.item_name.list'); } /** * @return \Illuminate\Http\RedirectResponse */ public function remove() { $this->name_translation->delete(); Alert::info('You have successfully deleted the item language.'); return redirect()->route('platform.item_name.list'); } } |
最终的结果如下图:
噢!评论已关闭。