Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel model global scope method in form model #5826

Open
jangidgirish opened this issue Sep 27, 2023 · 2 comments
Open

Laravel model global scope method in form model #5826

jangidgirish opened this issue Sep 27, 2023 · 2 comments

Comments

@jangidgirish
Copy link

  • Laravel Version: 9.52.15
  • PHP Version: 8.2.10
  • Laravel-admin: ^1.8

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
Copy link

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
Copy link
Author

jangidgirish commented Oct 1, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants