nova-issues icon indicating copy to clipboard operation
nova-issues copied to clipboard

Relatable validation error (query bindings issue)

Open SerhiiStarovoitov opened this issue 8 years ago • 10 comments

The issue is related to src/Rules/Relatable.php passes() method:

     /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        $model = $this->query->select('*')->whereKey($value)->first();

The passes() method is modifying $this->query with select('*') method and the original query is replaced by the new "select"-one, but if $this->query has already contains some bindings (from $with or $withCount original model properties, for example, in my case), they are not overwritten. So, as a result, we got a new query with old bindings and we're getting wrong query results (and passes method returns false so we're getting "Relatable validation error"). If the original query got no any bindings everything is fine.

SerhiiStarovoitov avatar Sep 26 '18 16:09 SerhiiStarovoitov

@SerhiiStarovoitov

I have the same issue, did you manage to solve the problem?

Sparclex avatar Nov 29 '18 14:11 Sparclex

I've just removed the "with" and "withCount" properties in {model}.php. @Sparclex

SerhiiStarovoitov avatar Nov 29 '18 14:11 SerhiiStarovoitov

Hmm for me issue exists, since I have a subquery which I prefer not to remove. Why does the query has to overwrite the selects. Could you not just remove the ->select('*')?

Sparclex avatar Dec 02 '18 18:12 Sparclex

@Sparclex: Can you check if the following code fixes the issue?

$model = (clone $this->query)->select('*')->whereKey($value)->first();

bonzai avatar Dec 02 '18 19:12 bonzai

@bonzai Sadly the issue still exists.

The following code would fix the issue:

// add this line, since the select is overwritten, the bindings should be reseted as well
$this->query->getQuery()->setBindings([], 'select'); 
$model = $this->query->select('*')->whereKey($value)->first()

Sparclex avatar Dec 03 '18 11:12 Sparclex

Sorry, but we are unable to debug your issue without a reproducible code example. Once you provide that we can open this up again.

heyhemp avatar Apr 05 '19 18:04 heyhemp

@davidhemphill

This happens when you have a Nova resource with an indexQuery with a ->withCount() on a polymorphic relationship.

E.g.:

App\AbuseReport

public function reportable()
{
    return $this->morphTo();
}

App\User

public function abuseReports()
{
    return $this->morphMany('App\AbuseReport', 'reportable');
}

App\Nova\User

public static function indexQuery(NovaRequest $request, $query)
{
    return $query
        ->withCount('abuseReports');
}

Now the passes() function in Laravel\Nova\Rules\Relatable fails because the bindings get messed up. Although the query does not contain the subselect for the count anymore, the bindings are not reset.

The prepared query:

select * from `users` where `users`.`id` = ? order by `users`.`id` desc limit 1`

The bindings:

[
  0 => 'App\User',
  1 => 2
]

The final query:

select * from `users` where `users`.`id` = 'App\\User' order by `users`.`id` desc limit 1

Which causes the passes() method to return false because no model can be found.

rbresjer avatar Jan 27 '20 13:01 rbresjer

@davidhemphill

Same issue on non polymorphic relationships when using $query->withCount('...') in indexQuery.

We are using this workaround https://github.com/laravel/nova-issues/issues/693#issuecomment-443684350 in production now.

decort-dev avatar Feb 27 '20 13:02 decort-dev

Same problem, I have App\Books class with the following:

public function authors()
{
        return $this->belongsToMany('App\Author');
}

While App\Author class has:

protected $withCount = ['books'];
public function books()
{
        return $this->belongsToMany('App\Book')->where('is_approved',true);
}

If i remove $withCount or remove where('is_approved',true) the attach will work. but keeping $withCount and is_approved clause gives the same error above.

the workaround by @Sparclex works for me.

ali-awwad avatar Mar 24 '20 16:03 ali-awwad

A bit late to the party, but i got the same problem.

Thanks to @rbresjer for explaining the issue and @Sparclex for providing a potential solution.

Here's what i did using that. I modified the relatableQuery() in the Nova Resource to this in my particular Nova Resource Classes:

    public static function relatableQuery(NovaRequest $request, $query)
    {
        $query->getQuery()->setBindings([], 'select')->select('*');
        return parent::relatableQuery($request, $query);
    }

I wish if $query->withCount([]) worked - that would be an even neater solution, but withCount() is doing an empty check and hence its does not work like we wanted.

It will basically remove other bindings from the query only when running a relatable query.

Hope this helps.

nikhilbaby2000 avatar Nov 02 '21 13:11 nikhilbaby2000