I’ve created an API using Laravel v5.8. This API has one endpoint that recieve some parameters and create 1 row on a database table. When I call to this endpoint using Postman, it works.
But… I’ve create an App using Laravel 5.8 and when this app try to call to that API endpoint sending the same parameters, and headers, this error is showed:
“IlluminateDatabaseQueryException: SQLSTATE[42S02]: Base table or view not found: 1146 Table  (truncated…”
That table should be ‘api_log’, but i can’t see this name in the error message.
NOTE: this error happens in the local environment only.
The Laravel app is using GuzzleHttp to call to the API.
APP request:
$client = new GuzzleHttpClient(); $res = $client->request('POST', 'http://project.local/v1/api_log', [ 'headers' => [ 'Content-Type' => 'application/x-www-form-urlencoded', 'Authorization' => $_ENV['API_TOKEN'] ], 'form_params' => [ 'json' => $json_request ] ]);
MySQL statement in API controller (no model is used):
$sql = "INSERT INTO api_log (user_id, environment, start_datetime, end_datetime, requested_api, processing_time, activity_id, ip_address, request_data, response_data, credits) VALUES (".$user_id.",'".$env."','".$start_datetime."','".$end_datetime."',".$requested_api.",".$processing_time.",'".$activity_id."','".$ip_address."','".$request_data."','".$response_data."',".$credits.")"; $insert_log = DB::connection('mysql_log')->statement($sql);
I’ve already tried to use a model like this but it didn’t work:
class ApiLog extends Model { protected $connection = 'mysql_log'; protected $table = 'api_log'; }
I’m using normal http://localhost/… domains for the API and APP. I’ve tried wrapping the API into a virtual host like http://project.local but the same error is showed.
Some clue? Thanks.
Advertisement
Answer
Well…after a lot of hours, I solved it watching the Laravel log file.
The problem was in .env file.
The .env file had this lines:
ALTER_CONNECTION=mysql_log ALTER_HOST=127.0.0.1 ALTER_PORT=3306 ALTER_DATABASE=my_db ALTER_USERNAME=root ALTER_PASSWORD=my_pass
And I’ve changed it by:
ALT_CONNECTION=mysql_log ALT_HOST=127.0.0.1 ALT_PORT=3306 ALT_DATABASE=my_db ALT_USERNAME=root ALT_PASSWORD=my_pass
I think these variables names were catched (I don’t know why) so the DB config was failing.
Thank you all.