0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

laravel backpackのメモ

Last updated at Posted at 2023-08-11

laravel backpackを使用すると高速で管理画面を開発することができて、自分用にメモとして残します。
今後、更新予定

meta title変更

 // CRUD::setHeading('新しいタイトル');
$this->crud->setHeading('テスト', 'edit');

第2引数を対応するアクション名にすることで、そのページのみ変更できる
setupアクションに記述する

ページタイトル(h1)変更

$this->crud->setTitle('Custom Page Title');

一覧画面のテーブルのラベルを変更する

thead > th相当
詳細画面などでもlabel変更可能

 protected function setupListOperation(){
   CRUD::column('name')->label('タイトル');
 }

日付のフォーマットを変更する

CRUD::column('created_at')->type('date')->format('YYYY-MM-DD H:mm:s')->label('編集日');

独自のパラメーターをviewに渡す

$this->data['hoge'] = 'テストパラメータ';

dataプロパティにキー名を設定する(githubに回答あり)
https://github.com/Laravel-Backpack/CRUD/issues/2616

オリジナルのviewに変更する

$this->crud->setListView('vendor.backpack.crud.tag_list');
// $this->crud->setShowView('your-view');
// $this->crud->setEditView('your-view');
// $this->crud->setCreateView('your-view');
// $this->crud->setReorderView('your-view');
// $this->crud->setDetailsRowView('your-view');

一覧view変更

@extends('crud::list')

@section('content')
{{-- @parent --}}

テスト

@endsection

更新が成功した際に独自処理をはさむ

use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation { update as traitUpdate; }
public function update($id)
    {
        //$this->crud->setValidation(TagRequest::class);

        // バリデーションが成功したことが確定した後の処理
        $response = $this->traitUpdate(); // このメソッドがバリデーションと更新の処理を行っています

        // 独自の処理をここに追加

        // 例: Log::info("エントリID {$id} が更新されました。");

        $text = "更新したのでメールを送信します。";

        Mail::raw($text, function ($message) {
            $message->from('from@example.com', 'Your App Name');
            $message->to('recipient@example.com');
            $message->subject('Your Subject Here');
        });
        

        return $response;
}

他の CRUD システム (GroceryCRUD など) を使用する開発者は、「before_insert」、「before_update」、「after_insert」、「after_update」を実行するコールバックを探すことになります。Backpack にはコールバックはありません。

backpackにはコールバックのようなものはないらしいのでupdateをオーバーライドして処理を追加する

チュートリアルcrud作成

laracasts/generatorsはインストール時にパスワードを求められるので有料?
なので普通にマイグレーションファイルを作成

#マイグレーションファイル作成
php artisan make:migration create_posts_table
php artisan migrate


#コントローラーなど実際のファイルを生成する
php artisan backpack:crud post

リレーション

記事に対して一つのタグを設定できるようにする例
普通のlaravelと一緒でリレーション設定するだけで自動的にリレーションされた値が出力される

マイグレーションファイル

public function up(): void
{
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tag_id')->nullable();
            $table->string('title');
            $table->text('content');
            $table->timestamps();

            $table->foreign('tag_id')->references('id')->on('tags')->onDelete('set null'); //タグが削除された場合nullにする
        });
}

リレーション設定

 public function tag()
    {
        return $this->belongsTo('App\Models\Tag', 'tag_id');
    }

手動で設定したい場合

 // CRUD::column('tag_id')->attribute('id');
CRUD::column('tag.name') // this is the related model's attribute we want to display
        ->label('Tag Name') // custom label for the column
        ->entity('tag') // the method in your Model that defines the relationship
        ->attribute('name') // the attribute that is shown to the user
        ->model("App\Models\Tag"); // foreign key model

ログイン画面 カスタマイズ

パスワードを忘れた方をなくす

/config/backpack/base.php

'setup_password_recovery_routes' => false,

ログイン画面の登録をなくす

'registration_open' => env('BACKPACK_REGISTRATION_OPEN', env('APP_ENV') === 'local'),

registration_openで定義されている
envファイルをAPP_ENVの値を「local」から「production」に変更する

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?