scramble icon indicating copy to clipboard operation
scramble copied to clipboard

Undefined variable when using array validation and variables.

Open dayio opened this issue 2 years ago • 2 comments

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 !

dayio avatar Jul 18 '24 09:07 dayio

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.

romalytvynenko avatar Jul 18 '24 10:07 romalytvynenko

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')

dayio avatar Jul 18 '24 11:07 dayio