> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# Form Request model retrieval and input boundaries

> How validation, authorization and query constraints apply when you retrieve models from input IDs in a Laravel 13 Form Request.

When you retrieve Eloquent models from IDs submitted in a form, you can use the Form Request methods `model`, `modelOrFail` and `models`. Building on the [requests guide](https://kawax.biz/en/requests.md), this page uses the implementation to clarify where these methods stop and input validation and authorization begin.

## Retrieve the model after validation

For example, when an authenticated user picks a project of their team by ID, first validate the input format in a Form Request.

```php theme={null}
namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class SelectProjectRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user() !== null;
    }

    public function rules(): array
    {
        return ['project_id' => ['required', 'integer']];
    }
}
```

Inject this request into the controller from a route that uses the authentication middleware. This example assumes the application has a `Project` model and a `view` policy.

```php theme={null}
use App\Http\Requests\SelectProjectRequest;
use App\Models\Project;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Gate;

public function show(SelectProjectRequest $request)
{
    $project = $request->modelOrFail(
        'project_id',
        Project::class,
        fn (Builder $query) => $query->where('team_id', $request->user()->team_id),
    );

    Gate::authorize('view', $project);

    return response()->json($project);
}
```

The Form Request is validated before the controller is called. When no matching record exists, `modelOrFail` throws a `ModelNotFoundException`, which normal HTTP handling turns into a 404. The team condition narrows the search, but it does not authorize the action on its own. Check the policy after retrieval as well.

> [!WARNING]
>
> These methods read the request input, not `validated()`. A Form Request's `authorize()` runs before validation, so if you call model retrieval inside it, the input has not been validated yet. When you use input in `authorize()`, treat it as unvalidated and design the validation and authorization you need separately.

## Return values and input differences

| Method | Input and result |
| - | - |
| `model` | Returns `null` for empty or array input, or when nothing matches. Otherwise returns the first match |
| `modelOrFail` | Returns the matching model. Throws `ModelNotFoundException` when nothing matches |
| `models` | Treats the input as an array and returns an Eloquent collection of the matching models |

`modelOrFail` does not treat an array of IDs as the ID of a single model either. `models` does not throw when some IDs are not found; it returns only the matches. When every ID must exist and be actionable, validate the input array and its elements, compare the results with the input, and authorize each model. Do not assume the results come back in input order.

## Query constraints and model inference

Retrieval starts from the model's `query()`, so Eloquent global scopes also apply. The closure's conditions are grouped in a `where`, and the primary key condition is added after it. This keeps OR conditions from leaking outside the ID condition, even when you use several `orWhere` calls.

When you omit the class, Laravel strips `_id` / `_ids` from the input key, infers a singular class name, and looks in the application's `Models` namespace, then the root namespace. When the names differ, such as `author_id` pointing to `User`, or when you work with a package's model, pass the class explicitly, such as `User::class`. The lookup uses the model's primary key, which is separate from the route key used by route model binding.

## Related

* [Form request validation](https://kawax.biz/en/validation.md)
* [Authorization](https://kawax.biz/en/authorization.md)
* [Official Laravel requests documentation](https://github.com/laravel/docs/blob/13.x/requests.md#retrieving-model-input-values)
* [InteractsWithModels implementation (v13.44.1)](https://github.com/laravel/framework/blob/v13.44.1/src/Illuminate/Foundation/Http/Concerns/InteractsWithModels.php)
* [Form Request validation and authorization order (v13.44.1)](https://github.com/laravel/framework/blob/v13.44.1/src/Illuminate/Validation/ValidatesWhenResolvedTrait.php)


## Related topics

- [Advanced Topics](https://kawax.biz/en/advanced/index.md)
- [HTTP requests](https://kawax.biz/en/requests.md)
- [⚡ Getting started with Livewire 4 — reactive UIs without JavaScript](https://kawax.biz/en/blog/livewire-introduction.md)
- [Handling signed URL input correctly](https://kawax.biz/en/signed-urls.md)
- [Amazon Bedrock driver for Laravel AI SDK](https://kawax.biz/en/packages/laravel-amazon-bedrock.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.