laravel-admin Controller grid、detail、form でリレーション → エクスポート機能どうなるか、その他Quick search機能
 
		  		        
      以下は、公式サイトよりサンプル抜粋しテストしてみました。
		  目次
		  
		  
		  
		
Grid 一覧表示でリレーション、コード→名称を取得
テーブルは、
authorsテーブル

booksテーブルで、外部キー books.author_id → author.id のリレーション

デフォルトの books 検索一覧画面

この一覧のbooks.author_id → author.first_name、author.last_nameを取得し表示する
リレーションで名称を取得して表示した画面

実装方法は、
Models > Book.php に以下のメソッドを追加
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
    use HasFactory;
    protected $fillable = [
        'title',
        'author_id',
        'page',
        'subtitle',
        'price'
    ];
    public function author() {
      return $this->belongsTo(Author::class);
    }
}
次に、
Controllers > BookController.php
を以下のように Grid に追加する。
    protected function grid()
    {
        $grid = new Grid(new Book());
        $grid->column('id', __('Id'));
        $grid->column('title', __('Title'));
        $grid->column('author_id', __('Author id'));
        // リレーション
        $grid->column('author.first_name', __('Author FirstName'));
        $grid->column('author.last_name', __('Author LastName'));
        $grid->column('page', __('Page'));
        $grid->column('subtitle', __('Subtitle'));
        $grid->column('price', __('Price'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));
        return $grid;
    }
次に detail でリレーション

こちらも girdと同様
    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Book::findOrFail($id));
        $show->field('id', __('Id'));
        $show->field('title', __('Title'));
        $show->field('author_id', __('Author id'));
        $show->field('author.first_name', __('Author FirstName'));
        $show->field('author.last_name', __('Author LastName'));
        $show->field('page', __('Page'));
        $show->field('subtitle', __('Subtitle'));
        $show->field('price', __('Price'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        return $show;
    }
最後に form で検索子画面を表示する
Selectable ディレクトリを作成し、

配下に Authors.php を新規作成する
<?php
namespace App\Admin\Selectable;
use App\Models\Author;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Grid\Selectable;
class Authors extends Selectable
{
    public $model = Author::class;
    public function make()
    {
        $this->column('id');
        $this->column('first_name');
        $this->column('last_name');
        $this->column('created_at');
        $this->filter(function (Filter $filter) {
            $filter->like('first_name');
            $filter->like('last_name');
        });
    }
}そして form に以下を追加する
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Book());
        $form->text('title', __('Title'));
        $form->belongsTo('author_id', Authors::class,'Author');
        $form->number('page', __('Page'));
        $form->text('subtitle', __('Subtitle'));
        $form->decimal('price', __('Price'));
        return $form;
    }

選択ボタンを押下すると、検索子画面が表示され、選択した物が反映される。よくあるリレーション。

ほんと簡単に実装できます。業務的なシステムがかなり効率よく作れますね。すごい!
その他、エクスポート機能はどうなる?

Grid で表示する内容がそのままエクスポート機能にも反映されました。Good!

その他、Grid に検索条件を追加してみる。Quick search
laravel-adminの公式ドキュメントより

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Book());
        $grid->quickSearch('title');
        $grid->column('id', __('Id'));
        $grid->column('title', __('Title'));
        $grid->column('author_id', __('Author id'));
        // リレーション
        $grid->column('author.first_name', __('Author FirstName'));
        $grid->column('author.last_name', __('Author LastName'));
        $grid->column('page', __('Page'));
        $grid->column('subtitle', __('Subtitle'));
        $grid->column('price', __('Price'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));
        return $grid;
    }
簡単ですね。その他、もっと調査やテストしてみたいと思います。
laravel-adminの次の調査としては、
- PDFやExcel出力する機能の実装
- リレーション(例:商品マスタの画面。カテゴリマスタなどのプルダウンやチェックボックス、ラジオボタンなど)機能の実装
PDF出力は、
- 
            前の記事  laravel-admin リレーション 1対多 のサンプルアプリを作る 2023.08.25
- 
            次の記事  laravel-admin で PDFサンプル出力。そしてGrid一覧画面の右端メニューをアイコンに設定変更 config/admin.php grid_action_class 2023.08.30