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

Leap_Chanvuthy's avatar

API Returns 200 OK but Resource Not Created in Laravel

Hi everyone,

I'm facing an issue where my SaleOrder store endpoint returns 200 OK, but the resource is not created in the database.

I have another similar repository for creating Product and associating RawMaterial in a many-to-many relationship, and it works perfectly. I tried to structure the SaleOrder store method similarly, but it does not behave as expected.

Here is the follow models that structured the relationships in my laravel API.

  1. SaleOrder Model :
  1. Product Model :
  1. ProductSaleOrder Model (Pivot Model)
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Product;
use App\Models\SaleOrder;

class ProductSaleOrder extends Model
{
    use HasFactory;
    protected $table = 'product_sale_orders';
    protected $fillable = [
        'product_id',
        'sale_order_id',
        'quantity_sold',
    ];

    public function product()
    {
        return $this->belongsTo(Product::class);
    }

    public function sale_order()
    {
        return $this->belongsTo(SaleOrder::class);
    }
}

Here is my controller logic to creat resource but it return status code 200 OK instead of status code 201 :

The Problem

  • The endpoint validates the data successfully.
  • It also runs without any errors and returns 200 OK or 201 Created.
  • However, the resource (SaleOrder) is not being created in the database, nor are the related - ProductSaleOrder records.
0 likes
13 replies
Snapey's avatar

temporarily remove the transaction

1 like
tykus's avatar

Are you doing anything to intercept thrown ModelNotFound or ValidationException exceptions (other than the catch blocks shown - aside catching a ValidationException appears to be redundant above)?

Have you tried to step-debug you code to verify that you are actually hitting this Controller, Action, and various statements therein?

Snapey's avatar

If you force a validation error, does it throw a validation exception as expected?

tykus's avatar

@leap_chanvuthy you need to step debug your code to find out where and how the Request is being handled; we can't see all of your application, only see what you have shared and the issue does not appear to be coming from the Controller action (directly).

If you don't have XDebug and cannot establish breakpoints in your code, you can use dd helper to kill execution at various points, e.g. making sure the Controller action is being hit:

public function store(Request $request)
{
    dd('in the controller action');
    $validated = $request->validate([
    // ...

Make sure validation passes:

public function store(Request $request)
{
    $validated = $request->validate([
    // ...
    ]);
    
    dd($validated);

And so on...

1 like
Leap_Chanvuthy's avatar

Guys I just found solution of my issue and I want to share if you also face the same problem as I am. The issue of the following code is the validation rule is outside the try block, Instead the code works if you write the validation rule on the try block.

Here is the correct version of my code :

tykus's avatar

@Leap_Chanvuthy that conclusion doesn't make sense in the context of the problem originally stated; a ValidationException normally returns a 422 status code, so where was the 200 coming from?

1 like
Leap_Chanvuthy's avatar

@tykus I don't know, it works for me and I happy that I can solve this issue. Make sense or not, Just wait until you face this same problem and try this solution.

Please or to participate in this conversation.