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.
@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 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
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!
@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
@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:
@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.
@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().
@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?
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.