I am not new to Laravel but I encounter a weird problem that never seen before. There are two projects on localhost named core implemented by Laravel and log implemented by Lumen. The core posts some data to log and log is responsible to store those data in its own database.
The is the core .env file:
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:rAfY jMXkAsPz wBrDksLNsPCLYzfNLI FISLfqZ/1s=
APP_DEBUG=true
APP_URL=http://localhost/core
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=
DB_PORT=3306
DB_DATABASE=core
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=localhost
REDIS_PASSWORD=
REDIS_PORT=6379
REDIS_EXPIRE=14400
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
and this is the .env file of log project
APP_NAME=Lumen
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost/log
APP_TIMEZONE=UTC
LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=log
DB_USERNAME=root
DB_PASSWORD=
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
Inside the log project there is a model Log
class Log extends Model
{
protected $fillable = ['entity_type', 'entity_id', 'user_id', 'metadata', 'action'];
protected $table = 'logs';
}
with this migration
class CreateLogsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('logs', function (Blueprint $table) {
$table->id();
$table->string('entity_type')->nullable();
$table->unsignedBigInteger('entity_id')->nullable();
$table->index(['entity_type', 'entity_id']);
$table->unsignedBigInteger('user_id');
$table->index('user_id');
$table->text('metadata')->nullable();
$table->enum('action', ['create', 'edit', 'delete', 'report']);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('logs');
}
}
when I run the migration it creates the table in the log database. The log contains only one function in a controller as follow
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'metadata' => 'sometimes|json',
'user_id' => 'required',
'action' => 'required|in:create,edit,delete,report',
'entity_id' => 'required',
'entity_type' => 'required',
]);
if ($validator->fails())
return response()->json(['error' => true]);
try {
Log::create($request->all());
return response()->json(['error' => false]);
} catch (\Throwable $e) {
return response()->json(['error' => $e->getMessage()]);
}
}
and finally this is the code which posts data from core to log via Guzzle inside a trait (I should mention that I also ran this code in controller too):
static::updated(function (Model $model) {
$data = [
'entity_type' => self::$tableModelMapper[$model->getTable()],
'entity_id' => $model->getAttribute('id'),
'user_id' => optional(auth()->user())->id,
'metadata' => json_encode($model->getDirty()),
'action' => 'edit'
];
$client = new Client();
$client->post('http://localhost/log/public/store',
[
'json' => $data
]);
});
The problem is:
When I post data from postman to log like this
{
"metadata":"{\"name\": \"Alex\",\"family\": \"Nicole\"}",
"user_id": 1,
"action": "create",
"entity_id": 12,
"entity_type":"Order"
}
it successfully creates the data in log database and logs table. But when I post from core to log by the code given above, it reports error
Server error:
POST http://localhost/log/public/storeresulted in a500 Internal Server Errorresponse: <!-- SQLSTATE[42S02]: Base table or view not found: 1146 Table 'core.logs' doesn't exist (SQL: insert intologs(`en (truncated...)
As you see, it says Table 'core.logs' doesn't exist while the logs table is in log database and the data insert to logs table is inside log project, then why core expects to have logs table?!!!
test
To find out why it happens I added the following codes `store` function of `log` project $result = DB::connection()->getDatabaseName();
\Illuminate\Support\Facades\Log::info($result);
when I post via postman it prints log in log file but when I post via core it prints core in log file. Thus when I post via core it tried to access the core database inside the log project!!
What is wrong here?
CodePudding user response:
maybe its because the nameserver is similar http://localhost/core and http://localhost/log the system will confuse are they different service or not.
i suggest you, to change the port number one of your service, example:
your logs to http://localhost:8080/log
