← PHP / Laravel · Contents

PHP-035 / Intermediate

Loose validation rules (accepting foreign keys without exists)

A01:2021 Broken Access Control · CWE-20 Improper Input Validation

Why is this a problem?

Associating category or team IDs owned by another user breaks data integrity and authorization.

How to fix it

php
$request->validate([
  'team_id' => ['required', Rule::exists('teams', 'id')->where('owner_id', $request->user()->id)],
]);

Further reading