Axeia
450
4
Laravel

Validating a request using 'exists' leading to SQL Error

Posted 6 months ago by Axeia

Hello,

My validation is as follows:

        $request->validate([
            'name' => 'required|exists:trainers,name',
            'id' => 'required',
            'id.*' => 'numeric|exists:'
        ]);

and my $_POST looks like this:

Array ( [_token] => 0I6lF7rLocDwy0rgargtZ48fAiSjHuaLpfmyAfSj [name] => Axxeia [id] => Array ( [0] => 27 [1] => 69 [2] => 107 ) )

When posting my form I get this error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'where `id`.`0` = ?' at line 1 (SQL: select count(*) as aggregate where `id`.`0` = 27)

Which makes sense as I expected the query to be:

select count(*) as aggregate from `trainers` where `name` = 'Axxeia'

The documentation states that the format is exists:table,column and as you can see in the screenshot below I have a trainers.name table/column as referenced to in the first code block above. Why is this failing?

table design showing the table and column do exists

Why is this going wrong, why does the query laravel runs seem to miss some vital information like a from clause? And how do I fix it?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.