1

I am trying to make an API with Laravel with the Northwind database.

Now I want to be able to send parameters in my URL so I can get a specific product with the id that I put in the URL. For example (http://localhost/NorthwindAPI/public/api/product/3).

When I'm trying this, laravel is giving an error saying that the column products.id does not exist which is correct because my column name is called ProductID.

How do I change the query so that I can filter on the right column name?

This is the exact error it gave me:

Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products.id' in 'where clause' (SQL: select * from products where products.id = 3 limit 1) in file D:\wamp64\www\NorthwindAPI\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 664

I would really appreciate someones help.

1 Answer 1

2

You need to change your primary key column. I'm going to assume you're using the Eloquent ORM, in which case:

class Product extends Eloquent {
  protected $primaryKey = 'ProductId';
}

It's worth mentioning that Laravel is quite opinionated, so you'd be much better off naming all your primary key columns 'id' which is what it's going to expect.

Similarly, Laravel 'likes' snake case for attributes, which is snake_case, not camel case, which is camelCase. I think you're using PascalCase, which is quite an unusual choice. If you're at a stage where you can reverse these design decisions, I strongly advise you to read up on the standards that Laravel is expecting, and unless you have a very good reason not, to stick to them.

More info can be found here:

https://laravel.com/docs/5.7/eloquent

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help. I found out i spelled $primaryKey without an capital letter K so thats why it wasnt working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.