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

Laravel model global scope method in form model #5826

Open jangidgirish opened 1 year ago

jangidgirish commented 1 year ago

Description:

The form model is not maintaining global scope methods in the quick update, i have called global scope disable method like $form->model()->withoutGlobalScope('status') but when i try to quick update status field from grid the code is throwing error No query results for model [App\Models\User] 16

Steps To Reproduce:

  1. Add global scope in model image
  2. disable global scope in form model image
  3. Add quick edit boolean switch in grid and enable / disable switch image
alexoleynik0 commented 1 year ago

This looks like not an issue to me, but rather expected behavior as you can't change query for Form's Model.

In Grid you can do it (as you probably did), but for the Form it takes Model and does findOrFail with all booted scopes etc, so you not only can't edit values within the Grid, but open Edit form / page for the Instance that is not-active (in your example) / not-found.

The only OK solution for this I can suggest is to use not the original Model class, but extended and modified AdminModel class, that doesn't have said global scopes registered. For example:

<?php

namespace App\Admin\Models;

use App\Models\User as Model;

class User extends Model
{
    protected static function booted()
    {
        // NOTE: empty for the override
    }
}

And in your App\Admin\Controllers\UserController use App\Admin\Models\User instead of App\Models\User.

jangidgirish commented 1 year ago

And in your App\Admin\Controllers\UserController use App\Admin\Models\User instead of App\Models\User.

thanks for your suggestion, this can work, but I am planning to use global scope in 4-5 models which will be extended only for admin task. i was expecting when it's working in grid and view model then it should also work as same in form model.