Laravel Orchid编辑列表中的条目

9lowa7mx  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(147)

我有一个列表,其中每个条目都有一个删除按钮,它和创建功能一样完美,我尝试添加一个编辑按钮,并且可以创建请求新名称的弹出窗口。
当我创建一个新条目并试图编辑它时,它会再次显示创建表单。当我试图编辑一个旧条目时,它会告诉我
哦,太棒了!更改一些内容,然后再次尝试提交。category.name字段是必填字段。
我的完整代码在这里,完成:

namespace App\Orchid\Screens;

use Orchid\Screen\Screen;
use Orchid\Screen\Fields\Input;
use Orchid\Support\Facades\Layout;
use Orchid\Screen\TD;
use Orchid\Screen\Actions\ModalToggle;
use App\Models\Category;
use Illuminate\Http\Request;
use Orchid\Screen\Actions\Button;

class CategoryScreen extends Screen
{
    /**
     * Fetch data to be displayed on the screen.
     *
     * @return array
     */
    public function query(): iterable
    {
        return [
            'categories' => Category::latest()->get(),
        ];
    }

    /**
     * The name of the screen displayed in the header.
     *
     * @return string|null
     */
    public function name(): ?string
    {
        return 'Category Screen';
    }

    /**
     * The screen's action buttons.
     *
     * @return \Orchid\Screen\Action[]
     */
    public function commandBar(): iterable
    {
        return [
            ModalToggle::make('category')
                ->icon('plus')
                ->method('create')
                ->modal('createCategory'),
        ];
    }

    /**
     * The screen's layout elements.
     *
     * @return \Orchid\Screen\Layout[]|string[]
     */
    public function layout(): iterable
    {
        return [
            Layout::table('categories', [
                TD::make('name'),

                // Create a delete button
                TD::make('Actions')
                    ->alignRight()
                    ->render(function (Category $category) {
                        return Button::make('')
                                ->icon('trash')
                                ->confirm(
                                    'After deleting, the category will be gone forever.'
                                )
                                ->method('delete', [
                                    'category' => $category->id,
                                ]);
                    }),

                TD::make('Actions')
                    ->alignRight()
                    ->render(function (Category $category) {
                        return Button::make('')
                            ->icon('pencil')
                            ->modal('editCategoryModal', [
                                'category' => $category,
                            ])
                            ->method('edit', [
                                'category' => $category->id,
                            ]);
                    }),
            ]),

            Layout::modal('createCategory', [
                Layout::rows([
                    Input::make('category.name')
                        ->title('Name')
                        ->placeholder('Enter category name'),
                ]),
            ])
                ->title('Create category')
                ->applyButton('Create'),
        ];
    }

    // Make a create method that validates name field
    public function create(Request $request)
    {
        $request->validate([
            'category.name' => 'required|max:255',
        ]);

        // Create a new category
        $category = new Category();
        $category->organisation_id = auth()->user()->organisation_id;
        $category->name = $request->category['name'];
        $category->save();
    }

    // Make a delete method that deletes the category
    public function delete(Request $request)
    {
        $category = Category::find($request->category);
        $category->delete();
    }

    // Make an edit method that validates name field
    public function edit(Request $request)
    {
        $request->validate([
            'category.name' => 'required|max:255',
        ]);

        // Update the category
        $category = Category::find($request->category->id);
        $category->name = $request->category['name'];
        $category->save();
    }

    public function editCategoryModal(): iterable
    {
        return [
            Layout::modal('editCategory', [
                Layout::rows([
                    Input::make('category.name')
                        ->title('Name')
                        ->placeholder('Enter category name'),
                ]),
            ])
                ->title('Edit category')
                ->applyButton('Save')
        ];
    }
}

我的网站,看起来:

当我按下编辑按钮(铅笔)时,表格:

我对Laravel Orchid还是个新手,所以我承认我可能用了完全错误的方法,但是文档中没有包含如何做的例子。谢谢。

jm2pwxwz

jm2pwxwz1#

您应该使用异步函数在同一页面上编辑模型(使用模态)Link:https://orchid.software/en/docs/modals/#asynchronous-data-loading
并且您还应该在layouts部分定义所有的modal,而不是在单独的函数中。

相关问题