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

majdiden's avatar

API post request not reaching the application

Hello, i'm new to laravel and i'm facing a painful problem.

I'm using Crinsane/LaravelShoppingcart in my ecommerce api and i'm trying to send a post request with axios in vuejs that adds a product to the cart by sending the product id and the quantity. The problem is the id and quantity are not reaching the application although i'm pretty sure i specified the correct route link in axios and i'm getting "No query results for model [App\Product]." which i assume means that the controller function that handles the request is working but the id is not being sent/transformed to the resource collection. I don't know if the problem is with the package i'm using or the code or something else.

I will provide the code upon request because i don't know which part exactly is needed for you in order to provide some help

Thank you in advance

0 likes
19 replies
frankielee's avatar

The meaning of the error is "No Such Record In Products Table"

Perhaps you have used this in your controller or route

Controller

//firstOrFail();
$model = Model::where('id',$request->id)->firstOrFail();
Will Redirect 404 with the message "No query results for model [App\Product] if no record found"

Route

Route::post('products/{product}','ProductsController@store');
Will Redirect 404 with the message "No query results for model [App\Product] if no record found"
majdiden's avatar

i'm using this in my controller

  $pdt = Product::findOrFail(request()->id);
  
  $cart = Cart::add([
    'id' => $pdt->id,
    'name' => $pdt->name,
    'qty' => request()->qty,
    'price' => $pdt->price
  ]);
frankielee's avatar

So your API and controllers are working correctly. Just need to make sure the product is existing in your database.

majdiden's avatar

tried to check using this

if ($request->get('id')) {
        $pdt = Product::find($request->get('id'));
}

        if ($request->get('qty')) {
          $qty = $request->get('qty');
        }

but it now shows a 500 error when i try to dd the cart

frankielee's avatar

You can check all the parameters passed by using

dd($request->all());

Update: What is the error?

majdiden's avatar

"Undefined variable: cart"

update: dd() is not even working

frankielee's avatar

First thing first, please make sure the product exists in your database before doing rest.

Example

if(!$product =Product::find($id))
      return "not found";

Show me the full codes of the function.

majdiden's avatar

this is the full controller code

<?php

namespace App\Http\Controllers;
use App\Product;
use Illuminate\Http\Request;
use Cart;
use App\Http\Resources\CartCollection;
class ShoppingController extends Controller
{
    public function store(Request $request){

      if ($request->get('id')) {
        $pdt = Product::find($request->get('id'));
}
        if ($request->get('qty')) {
          $qty = $request->get('qty');
        }

      $cart = Cart::add([
        'id' => $pdt->id,
        'name' => $pdt->name,
        'qty' => $qty,
        'price' => $pdt->price
      ]);

      
      dd($request->all());
    }

}
frankielee's avatar

Try dump($request->all()); first before the rest.

Where does your Cart model file located? It should be use App\Cart; instead?

majdiden's avatar

it didn't work also.

I only created one model for the products and i'm using the package for the cart functionality.

this code worked fine when i tried it with a blade template and placed the route in web.php :

 $pdt = Product::findOrFail(request()->id);
  
  $cart = Cart::add([
    'id' => $pdt->id,
    'name' => $pdt->name,
    'qty' => request()->qty,
    'price' => $pdt->price
  ]);

I suppose this means that the product exists in the DB

majdiden's avatar

i'm not using blade, i'm using Vue .. I only used a blade template to test if the function works or not and it worked.

frankielee's avatar

Few things to clarify:

  1. Does the data passed successfully?

  2. Does the product exists?

  3. Does the Cart created successfully?

  4. How does you handle the http request in Vue?

majdiden's avatar

1- the data is not being passed successfully

2- these are my products :

    "data": [
        {
            "id": 1,
            "name": "MEN'S BETTER THAN NAKED & JACKET",
            "description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\n                  tempor incididunt ut labore et dolore magna aliqua consequat.",
            "price": 200.1,
            "quantity": 21,
            "sold": 0,
            "remaining": 0,
            "rating": 0,
            "bestSelling": 0,
            "featured": 0
        },
        {
            "id": 2,
            "name": "WOMEN'S BETTER THAN NAKED™ JACKET",
            "description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\n                  tempor incididunt ut labore et dolore magna aliqua consequat.",
            "price": 1600.21,
            "quantity": 400,
            "sold": 0,
            "remaining": 0,
            "rating": 0,
            "bestSelling": 0,
            "featured": 0
        },
        {
            "id": 3,
            "name": "WOMEN'S SINGLE-TRACK SHOE",
            "description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\n                  tempor incididunt ut labore et dolore magna aliqua consequat.",
            "price": 378,
            "quantity": 37,
            "sold": 0,
            "remaining": 0,
            "rating": 0,
            "bestSelling": 0,
            "featured": 0
        },
        {
            "id": 4,
            "name": "Enduro Boa® Hydration Pack",
            "description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\n                  tempor incididunt ut labore et dolore magna aliqua consequat.",
            "price": 21.1,
            "quantity": 10,
            "sold": 0,
            "remaining": 0,
            "rating": 0,
            "bestSelling": 0,
            "featured": 0
        },
        {
            "id": 5,
            "name": "Home",
            "description": null,
            "price": 51,
            "quantity": 51,
            "sold": 51,
            "remaining": 51,
            "rating": 0,
            "bestSelling": 1,
            "featured": null
        }
    ]

this is how i handle http requests in vue using axios :

addCart(item) {
                 axios
                 .post('/api/cart/add', item)
                 .then(response => (response.data.data))
                 .catch(error => console.log(error.response.data))
}

the http request hits this api route :

Route::post('cart/add', [
  'uses' =>  'ShoppingController@store',
  'as' => 'cart.add'
]);

-- UPDATE --

this is what i'm sending :

{__ob__: Observer}
id: 5
qty: "2"

and the controller above handles the request

in blade the cart is being created successfully but when i try using the api it doesn't work.

frankielee's avatar

The code looks good to me, if you open the browser developer tools, any error is shown?

frankielee's avatar

Btw, maybe you can add the header on the request

axios..post('/api/cart/add', item)
{
  headers: {
    Content-Type: 'application/json'
  }
})
majdiden's avatar

if im using this :

  public function store(Request $request){

      if ($request->get('id')) {
        $pdt = Product::find($request->get('id'));
}
        if ($request->get('qty')) {
          $qty = $request->get('qty');
        }

      $cart = Cart::add([
        'id' => $pdt->id,
        'name' => $pdt->name,
        'qty' => $qty,
        'price' => $pdt->price
      ]);

      
    }

i get neither errors nor data

and when i use this one :

 $pdt = Product::findOrFail(request()->id);
  
  $cart = Cart::add([
    'id' => $pdt->id,
    'name' => $pdt->name,
    'qty' => request()->qty,
    'price' => $pdt->price
  ]);

i get "No query results for model [App\Product]."

frankielee's avatar

I found that the .then(response => (response.data.data)) is not logging anything, could it be the issue?

console.log(response.data);

majdiden's avatar

it's not logging any data because there's no response

Please or to participate in this conversation.