z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.15k stars 2.82k forks source link

Form deleting callback model is null #5609

Closed k0hel closed 2 years ago

k0hel commented 2 years ago

Description:

I want to get id of the deleting records. but model return null. oddly enough, it works fine at submitted, saving, saved.

Steps To Reproduce:

$form->deleting(function () use($form) {
            $model = $form->model(); //return null
        });
hoangnamitc commented 2 years ago

Try

$form->deleting(function ($form) {
    dd(
       $form->id
     );
})
k0hel commented 2 years ago

dd($form->id); null

dd($form->model());

App\Models\Test {#1580
  #connection: "mysql"
  #table: "test"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: []
  #original: []
  #changes: []
  #casts: array:1 [
    "deleted_at" => "datetime"
  ]
  #classCastCache: []
  #attributeCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [
    0 => "*"
  ]
  #forceDeleting: false
}
hoangnamitc commented 2 years ago
image

Read: https://laravel-admin.org/docs/en/model-form-callback

k0hel commented 2 years ago

same $form->model()->id; null

I already know that document and code. It works with respect to submitted, saving and saved mentioned. not work for deleting and deleted. deleting and deleted are not documented, so I don't know if it's actually a bug or not. deleted method seems to be called after deleting the model, so I think the absence of the model is the right move. but, deleting method seems to be called before deleting the model, it should be able to refer to the model.

k0hel commented 2 years ago

couldn't get the model, but could get the primary key id.

$form->deleting(function ($form,$id) {
    Log::debug($id);
});