Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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\Nova\Fields\HasOne constructor does not exclude global scopes #5955

Closed
fjcantais opened this issue Oct 11, 2023 · 0 comments
Closed
Labels
pending Issues that are pending triage

Comments

@fjcantais
Copy link

  • Laravel Version: 10.13.5
  • Nova Version: 4.25.1
  • PHP Version: 8.1.3
  • Database Driver & Version: MySQL 5.7.31
  • Operating System and Version: Windows 10.0.19045
  • Browser type and version: Google Chrome 117.0.5938.149
  • Reproduction Repository: N/A

Description:

Laravel\Nova\Fields\HasOne constructor does not exclude global scopes. Therefore, if a global scope excludes a certain model instance from the query, it is impossible to assign a HasOne relationship. Nova throws an 'Attempt to read "[relationship_here]" on null'.

Current HasOne code:
$parent = $parentResource::newModel() ->with($this->attribute) ->find($request->viaResourceId);

Suggested correction:
$parent = $parentResource::newModel() ->withoutGlobalScopes() ->with($this->attribute) ->find($request->viaResourceId);

Detailed steps to reproduce the issue on a fresh Nova installation:

  1. Create a global scope, for instance a PublishableScope with this method:
    public function apply(Builder $builder, Model $model) { $builder->where('published', true); }
  2. Define an Eloquent model with a published boolean attribute, say "Customer"
  3. Create an instance of this Eloquent model with a value of published=false.
  4. Create a second Eloquent model, say "Address" and define a one-to-one relationship between them.
  5. Create a Nova resource for each of those models. Do not forget to define a HasOne field in the first resource (Customer) pointing to the second resource (Address).
  6. Open the detail view of the unpublished Customer resource and try to assign it an Address. The error 'Attempt to read "address" on null' will arise.
@crynobone crynobone added the pending Issues that are pending triage label Oct 16, 2023
@laravel laravel locked and limited conversation to collaborators Feb 16, 2024
@crynobone crynobone converted this issue into discussion #6213 Feb 16, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
pending Issues that are pending triage
Projects
None yet
Development

No branches or pull requests

2 participants