Laravel-adminで store() を拡張する。
Laravel-adminを導入して見たんですが丁寧なマニュアルがない…。 新規登録ボタンで別のDBにも登録できるようにしたのでその時のやり方。
AdminController.php
use App\Models\AdminModel; class AdminController extends Controller { //trait ModelForm内のstoreを上書き public function store(Request $request) { //Modelクラスをインスタンス化して自作メソッド呼び出し $AdminModel = new AdminModel; $AdminModel->insertAnotherDB($request); //Formクラス内のstore()を呼び出し //Laravel-adminの標準はこれだけを実行する return $this->form()->store(); } }
Models\AdminModel.php
use App\Models\AnotherModel; class AdminModel extends Model { protected $table = 'another_db'; /** * 別DBへのインサート */ public function insertAnotherDB($request) { //別DBのモデルをインスタンス化 $AnotherModel = new AnotherModel; //requestから値をセット $AnotherModel->name = $request->name; $AnotherModel->password = $request->password; //DBに保存 $AnotherModel->save(); } }
めちゃくちゃ簡単だけどGithubのdocsにこのやり方書いてないんだよね……。 parent:: 使ってやってみてもそもそもどういう動作してるかわからないから結局 調べなきゃならなかった。












