laravel-mongodb
laravel-mongodb copied to clipboard
Setting prefix doesn't work
- Laravel-mongodb Version: 3.9.0
- PHP Version: 8.0.15
- Database Driver & Version: 4.0.4
Description:
I set prefix in config/database.php file, but he doesn't work. My database table names are all prefixed, but I set them up, but it doesn't work, which makes me very distressed. hope it helps me
Steps to reproduce
- Set
ll_prefixinconfig/database.php`.
return [
'connections' => [
'mongodb_log' => [
'driver' => 'mongodb',
'url' => env('DATABASE_URL'),
'host' => explode(',', env('DB_HOST', '127.0.0.1')),
'port' => env('DB_PORT', '27017'),
'database' => env('DB_LOG', ''),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'prefix' => 'll_',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => [
'database' => env('DB_MONGODB_AUTH_DATABASE', 'admin'),
],
],
]
];
- Create a model class file.
<?php
declare(strict_types=1);
namespace App\Models\Mongodb\Log;
use Jenssegers\Mongodb\Eloquent\Model;
class LogBootTimeLog extends Model
{
/**
* The connection name for the model.
*
* @var string
*/
protected $connection = 'mongodb_log';
/**
* The collection associated with the model.
*
* @var string
*/
protected $collection = 'log_boot_time_log';
}
- Insert data into
ll_log_boot_time_log
<?php
use App\Models\Mongodb\Log\LogBootTimeLog;
$data = [
'mac_address' => '00:00:00:00',
'machine_id' => 1,
'runtime' => 10,
'touch_time' => time(),
'add_time' => time(),
];
LogBootTimeLog::insert($data);
- The resulting sql statement.
log_boot_time_log_202203.insertMany([{"mac_address":"00:00:00:00","machine_id":1,"runtime":10,"touch_time":1647454088,"add_time":1647454088}])
This is not the result I want, I hope the prefix set will work.
I want to effect:
ll_log_boot_time_log_202203.insertMany([{"mac_address":"00:00:00:00","machine_id":1,"runtime":10,"touch_time":1647454088,"add_time":1647454088}])
Expected behaviour
I want to be able to set the prefix to make it take effect.
Actual behaviour
I want to be able to set the prefix to make it take effect.
same problem
same problem
I created a model in ./app/Models/Model.php like this:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Jenssegers\Mongodb\Eloquent\Model as Model_;
class Model extends Model_
{
use HasFactory;
public function __construct() {
$this->collection = env("DB_PREFIX") . parent::getTable();
}
}
Then other models extend from this model like this for example:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Models\Model;
class Post extends Model
{
use HasFactory;
}
It's worked for me.