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

Faiza's avatar
Level 1

Creating an API

Hi

I'm trying to create an API using my existing database. I've tried following a tutorial from this website: https://qadrlabs.com/post/rest-api-authentication-with-laravel-sanctum. It works wonder if I use pre created user model but, if I create my own model based on my database I get this error when I test it on postman

Illuminate\Database\QueryException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'nwmsystem.users' doesn't exist (SQL: select * from users where username = hwa limit 1) in file C:\xampp\htdocs\NWMapi\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 759

I've try to look into Connection.php file but there's no any query. I wonder where is this query (SQL: select * from users where username = hwa limit 1) is located

Is there any way I can fix this? Or is there any better approach to make an API that i should try?

This is my first time creating an API and using Laravel.

0 likes
22 replies
frankielee's avatar

Make sure you have created the migration the table is created?

Faiza's avatar
Level 1

@frankielee I want to use the table that I already created in my database. But my problem is i dont know where this query was written. So I cannot change it (SQL: select * from users where username = hwa limit 1)

Sinnbeck's avatar

What is does the query look like in your code? And what is the users table called in the existing database.

Faiza's avatar
Level 1

@Sinnbeck I didn't write any query in my model or controller so I too wonder where is that query was written. I try to look into the Connection.php file as per written in the error but there's no query was written in that file. I think that query was pre written while I created the project file but I seriously didn't know it was written in which file

Sinnbeck's avatar

@Faiza You can check the stack trace to see where in your code it is called :) Or share the error here (there is a share button)

And can you show your user model that fails, and tell us what your users table is called?

OussamaMater's avatar

@Faiza did you manually create the tables of each model? without running any migrations? because Laravel could not find any table called users.

Faiza's avatar
Level 1

@OussamaMater Yes. I use my existing database. I've created new migration base on table in my database

Faiza's avatar
Level 1

@OussamaMater I already run the migration and it give me this response

Nothing to migrate.

Sinnbeck's avatar

@Faiza The thing on the left side with the numbers is the stack trace. The trick is to look for a line starting with /app. In your case its line 39.

https://flareapp.io/share/NPG6yznm#F39

Just so there are no misunderstandings. When you said "using my existing database" does that mean that it already have all the tables needed with data ?

If you alrady have the tables and data, you dont need migrations for now!

Faiza's avatar
Level 1

@Sinnbeck Thankfully I manage to handle that problem already. I change post method to get method in my api route. But now, I have another problem. How can I create token for existing user? My app was supposed no registration step since login information was already there in the database. I mean, username and password needed was already in the database

OussamaMater's avatar

@Faiza Great that you solved your issue, please share the steps for the future comers, that might help someone. For your question, you may one of Laravel's packages (Sanctum or Passport), I recommend sanctum as this is your first time creating an api, and it's quite simple, follow up this docs:

  1. https://laravel.com/docs/9.x/sanctum#installation
  2. https://laravel.com/docs/9.x/sanctum#issuing-api-tokens

It's in order, you need to first install the package and run its migrations, and then start issuing tokens.

Faiza's avatar
Level 1

@OussamaMater I just refer to this tutorial: https://qadrlabs.com/post/rest-api-authentication-with-laravel-sanctum. But instead of using User model, I create another model based on my database table and I change post method to get method for login in api route. This tutorial use sanctum for authentication. But it assign token when the user register. For my case, user no need to register. They will login user data that was already available in database and that's what I need to working on. How to set token to the existing user.

OussamaMater's avatar

@Faiza Yes that's exactly what I shared with you, you can issue a token to an existing user by using the createToken() method, you just need to make sure your Model uses the HasApiTokens trait, the method will work on an model instance so fetch the "existing" user you want from the database and then call createToken().

Faiza's avatar
Level 1

@OussamaMater You mean I need to add createToken() method in login funtion in the controller?

OussamaMater's avatar
Level 37

@Faiza yes exactly, I have the perfect tutorial for your you, it will clear a lot of conflicts.

if you want to skip, you can go to minute 48, that's what you're looking for.

Or if you want to skip the whole video, here's my code, that has createToken() in the login.

Check the authenticate() method.

Faiza's avatar
Level 1

@OussamaMater Okay I'll take a look on it. Thank you so much for your help sir. May you have a good day ahead :)

1 like
OussamaMater's avatar

@Faiza Thanks, I updated the answer and provided some code, have a look, hopefully that helps you.

1 like
Faiza's avatar
Level 1

@OussamaMater Hi sir, I'm currently trying to create a logout function. I try to do just like how you did in the github file that you give last time. I want to ask why got error for tokens()? The error said "Undefined method". Can you help me where should I define it. And one more this if I not use User.php model do I need to change user() to something else?

    {
        auth()->user()->tokens()->delete();

        return $this->returnResponse('success', 'user logged out.', 200);
    } 
OussamaMater's avatar

@faiza what do you mean by

I've created new migration base on table in my database did you already have the tables and then wrote the migrations? because it doesn't work that way, am pretty sure the issue is related to the migrations not being run correctly maybe try

php artisan migrate:fresh

please not that this command will drop all the tables and re-create them, so if you do have some data that you need back it up.

Please or to participate in this conversation.