mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/docs/drivers/php/laravel-mongodb/
MIT License
7.02k stars 1.43k forks source link

Setting prefix doesn't work #2372

Open peibinzhu opened 2 years ago

peibinzhu commented 2 years ago

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

  1. 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'),
            ],
        ],
    ]
];
  1. 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';
}
  1. 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);
  1. 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.

yexk commented 2 years ago

same problem

farzam74 commented 1 year ago

same problem

iamkeng commented 1 year ago

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.