Outputting related fields in Laravel Orchid?

153 Views Asked by At

There is a database data structure. In the Orchid admin panel, I need to display the data from the type_goods table on the screen. enter image description here

I'm writing a screen to display data.

class TypeGoodScreen extends Screen
{
    /**
     * Fetch data to be displayed on the screen.
     *
     * @return array
     */
    public function query(): iterable
    {
        $goods = TypeGoods::filters()->
        defaultSort('id', 'asc')->
        paginate();
        return [
            'goods' => $goods
        ];
    }

...

   /**
     * The screen's layout elements.
     *
     * @return \Orchid\Screen\Layout[]|string[]
     */
    public function layout(): iterable
    {
        return [
            TypeGoodListLayout::class,
        ];
    }
}

In the next screen (TypeGoodListLayout.php) I write this

class TypeGoodListLayout extends Table
{
    public $target = 'goods';

    /**
     * @return TD[]
     */
    public function columns(): array
    {
        return [
            TD::make('good_id', 'Category of good')->render(function (Goods $good) {
                return $good->title." (".$good->id.")";
            })
                ->sort()
                ->cantHide()
                ->filter(Input::make()),

            TD::make('name', 'Good')
                ->sort()
                ->cantHide()
                ->filter(Input::make()),

            TD::make('image', 'image')->render(function (Goods $goods){
                if (!empty($goods->image))
                    return "<img style='width:50px' src=".$goods->image."";
                else
                    return "-";
            }),

            TD::make('updated_at', "Date updated")
                ->sort()
                ->render(fn (TypeGoods $good) => $good->updated_at->toDateTimeString()),

            TD::make(__('Actions'))
                ->align(TD::ALIGN_CENTER)
                ->width('100px')
                ->render(fn (TypeGoods $good) => DropDown::make()
                    ->icon('bs.three-dots-vertical')
                    ->list([

                        Link::make(__('Edit'))
                            ->route('platform.type-goods.edit', $good->id)
                            ->icon('bs.pencil'),

                        Button::make(__('Delete'))
                            ->icon('bs.trash3')
                            ->confirm(__('Remove this record?'))
                            ->method('remove', [
                                'id' => $good->id,
                            ]),
                    ])),
        ];
    }

It is not possible to display fields from related tables goods and images. How to display connections correctly? Example, fields goods.title or image.image.

0

There are 0 best solutions below