Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

wonder95's avatar

Resource Controller PATCH - no query result for model

Using the following route definition

Route::group(['middleware' => ['auth:sanctum', 'clean-input']], function () {
    Route::resource('custom-field-definition', 'CustomFieldDefinitionController')->except([
        'edit',
        'create'
    ])
        ->middleware('bind-workspace-model');
});

for a CustomFieldDefinitionModel with all of this route wrapped in

Route::group(['prefix' => 'workspace/{workspace}'], function () {}

in the /api folder, makes the route to the update URL

/api/workspace/{workspace}/custom-field-definition/{custom_field_definition}

Mu update() method in my controller is defined as

    public function update(CustomFieldModelUpdateRequest $request, WorkspaceModel $workspaceModel, CustomFieldDefinitionModel $customFieldDefinition){}

However, when I call my URL with

http:://web.test:8080/api/workspace/66df3c71b09e2e011e0c7113/custom-field-definition/6716e3b6af628e457606e292

I get an error in my console

"No query results for model [App\\Models\\CustomFieldDefinitionModel] 6716e3b6af628e457606e292"

where the 6716e3b6af628e457606e292 id is a valid id in my db.

It should be noted that the index (GET) and store (POST) routes work fine, so it's just the update route` having problems.

What am I missing?

0 likes
6 replies
LaryAI's avatar
Level 58

The error message "No query results for model [App\Models\CustomFieldDefinitionModel]" indicates that Laravel's route model binding is unable to find a CustomFieldDefinitionModel instance with the given ID. Here are a few steps to troubleshoot and resolve this issue:

  1. Check Route Model Binding: Ensure that the CustomFieldDefinitionModel is correctly set up for route model binding. By default, Laravel uses the id column for binding. If your model uses a different column (e.g., uuid), you need to specify it in the model:

    public function getRouteKeyName()
    {
        return 'uuid'; // or the column you are using
    }
    
  2. Verify Middleware: Since you are using a custom middleware bind-workspace-model, ensure that it does not interfere with the model binding process. Double-check the logic in this middleware to ensure it doesn't inadvertently affect the CustomFieldDefinitionModel binding.

  3. Check Database Records: Confirm that the record with ID 6716e3b6af628e457606e292 exists in the database and is accessible. You can do this by running a simple query in a tinker session or directly in your database client:

    $record = \App\Models\CustomFieldDefinitionModel::find('6716e3b6af628e457606e292');
    dd($record);
    
  4. Route Parameters: Ensure that the route parameters are correctly defined and match the expected order in your controller method. The parameters in the route should match the order in the controller method signature:

    public function update(CustomFieldModelUpdateRequest $request, WorkspaceModel $workspaceModel, CustomFieldDefinitionModel $customFieldDefinition)
    
  5. Check Route Definition: Verify that the route definition is correct and matches the expected URL structure. Ensure that the Route::resource is correctly set up to handle the PATCH method for updates.

  6. Debugging: Add some debugging statements in your update method to log the incoming parameters and check if they are being passed correctly:

    public function update(CustomFieldModelUpdateRequest $request, WorkspaceModel $workspaceModel, CustomFieldDefinitionModel $customFieldDefinition)
    {
        \Log::info('Workspace ID:', [$workspaceModel->id]);
        \Log::info('Custom Field Definition ID:', [$customFieldDefinition->id]);
        // Your update logic here
    }
    

By following these steps, you should be able to identify the root cause of the issue and resolve the error. If the problem persists, consider checking the Laravel documentation on route model binding for any additional configuration that might be necessary.

vincent15000's avatar

Can you show the migration and the model please ? I want to check how you declare the primary key.

wonder95's avatar

Here is the model

No migration written yet, it's a MongoDB database.

1 like
vincent15000's avatar

@wonder95 You necessarily have a structure for your database, even if you are using MongoDB (although it would be better in your case to use a relational database).

You are trying to retrieve a model using the model binding. But what field is used to be the primary key ?

$record = \App\Models\CustomFieldDefinitionModel::find('6716e3b6af628e457606e292');

6716e3b6af628e457606e292 is not an integer type. When you are using a different type than integer for the primary key, you need to declare your primary key type in your model.

https://laravel.com/docs/11.x/eloquent#primary-keys

If your model's primary key is not an integer, you should define a protected $keyType property on your model. This property should have a value of string:

<?php
 
class Flight extends Model
{
    /**
     * The data type of the primary key ID.
     *
     * @var string
     */
    protected $keyType = 'string';
}

Furthermore your primary key is not incrementing, you should also specify it.

public $incrementing = false;

And if your primary key name is different than id, you need to define it in the model.

protected $primaryKey = 'flight_id';

Can you show the update route please ?

tykus's avatar

Nested resource routes are automatically scoped so that the child Model must be associated with the parent Model - you have no such relationships established as far as i can see (maybe in the Trait?) https://laravel.com/docs/11.x/controllers#scoping-nested-resources

You have also created an issue where the route-model binding will not work, e.g. route wildcard parameter workspace does not match the Controller action argument $workspaceModel

1 like
Tray2's avatar

You really should consider using a relational database instead, NoSQL databases serves their purpose, but in almost all cases a relational database is the better choice.

1 like

Please or to participate in this conversation.