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

Undefined variable when using array validation and variables. #460

Closed
Bouhnosaure opened this issue Jul 18, 2024 · 2 comments
Closed

Undefined variable when using array validation and variables. #460

Bouhnosaure opened this issue Jul 18, 2024 · 2 comments

Comments

@Bouhnosaure
Copy link

Hi !

I've an issue where the documentation cannot be generated, because variables aren't detected when the code is parsed, i've seen a PR #237 but i don't know if it's in progress or abandoned.

For this code for instance :

	
  $customer = Customer::findOrFail($id),
  
  $data = $request->validate([
  	'reference' => ['required', 'string', Rule::unique('orders')->where($customer->id, 'customer_id')]
	...

I have this behavior :

image

Thanks for this project, it's really cool !

@romalytvynenko
Copy link
Member

Yo @Bouhnosaure! It is indeed would be fixed in #237!

As a workaround you can inject a customer, if it is possible:

// routes/api.php
Route::update('customers/{customer}', [CustomerController::class, 'update']);

// app/Http/Controllers/CustomerController.php
class CustomerController
{
    public function update(Request $request, Customer $customer)
    {
        $data = $request->validate([
            'reference' => ['required', Rule::unique('orders')->where($customer->id, '...')]
        ]);
    }
}

I get that this may be not possible for you, but in case it is injected, Scramble should not fail.

@Bouhnosaure
Copy link
Author

Bouhnosaure commented Jul 18, 2024

Thanks for you answer, but i use a macro for it which is retrieved from the request first, because i have some data retrieved from a middleware.

Now that you've exposed your example, as a workaround i'll use the $request variable directly without assignation to a variable first.

And indeed it's working with $request->customer()?->id but need to use the null safe operator, otherwise i have an undefined property 'id')

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