Skip to content

Commit

Permalink
[CHN] how to get current db connection
Browse files Browse the repository at this point in the history
  • Loading branch information
moaminsharifi committed Feb 11, 2024
1 parent 472f3c7 commit 424a117
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 30 deletions.
18 changes: 11 additions & 7 deletions config/easy_panel_config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

use EasyPanel\Services\LangService;
use EasyPanel\Support\Auth\AdminIdentifier;
use EasyPanel\Support\User\UserProvider;

return [

// Enable whole module
Expand All @@ -19,13 +23,13 @@

// How to authenticate admin
// You may use other ways to authenticate a admin (tables or ..) you can manage it with this class
'auth_class' => \EasyPanel\Support\Auth\AdminIdentifier::class,
'auth_class' => AdminIdentifier::class,

// With this class you can manage how to create a admin or remove it.
'admin_provider_class' => \EasyPanel\Support\User\UserProvider::class,
'admin_provider_class' => UserProvider::class,

//The namespace of lang manager class
'lang_manager_class' => \EasyPanel\Services\LangService::class,
'lang_manager_class' => LangService::class,

// it's a place where a user if not authenticated will be redirected
'redirect_unauthorized' => '/',
Expand All @@ -43,9 +47,9 @@
'lazy_mode' => true,

// database configure
'database'=>[
'connection'=> env('EASY_PANEL_DB_CONNECTION'),
'panel_admin_table'=>'panel_admins',
'crud_table'=> 'cruds'
'database' => [
'connection' => env('EASY_PANEL_DB_CONNECTION', env('DB_CONNECTION', 'mysql')),
'panel_admin_table' => 'panel_admins',
'crud_table' => 'cruds'
]
];
2 changes: 1 addition & 1 deletion database/migrations/cruds_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CreateCrudsTableEasypanel extends Migration
*/
public function getConnection()
{
return config('easy_panel.database.connection') ?: config('database.default');
return config('easy_panel.database.connection');
}

/**
Expand Down
3 changes: 2 additions & 1 deletion database/migrations/panel_admins_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ class CreatePanelAdminsTableEasypanel extends Migration
*/
public function getConnection()
{
return config('easy_panel.database.connection') ?: config('database.default');
return config('easy_panel.database.connection');
}

/**
* Run the migrations.
*
Expand Down
3 changes: 1 addition & 2 deletions src/EasyPanelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ private function loadLivewireComponent()
private function isDBConnected()
{
try {
$connection = config('easy_panel.database.connection') ?: config('database.default');
DB::connection($connection)->getPDO();
DB::connection(config('easy_panel.database.connection'))->getPDO();
} catch (Exception $e) {
Log::error('Please check your DB connection \n Can not load routes of EasyPanel');
Log::error($e->getMessage());
Expand Down
13 changes: 5 additions & 8 deletions src/Models/CRUD.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,22 @@

class CRUD extends Model
{
protected $guarded = [];

/**
* Create a new Eloquent model instance.
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
$connection = config('easy_panel.database.connection') ?: config('database.default');
$table = config('easy_panel.database.crud_table') ?: 'cruds';

$this->setConnection($connection);

$this->setTable($table);

$this->setConnection(config('easy_panel.database.connection'));
$this->setTable(config('easy_panel.database.crud_table'));

parent::__construct($attributes);
}

protected $guarded = [];

public function scopeActive($query)
{
return $query->where('built', true)->where('active', true)->get();
Expand Down
19 changes: 8 additions & 11 deletions src/Models/PanelAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,22 @@
class PanelAdmin extends Model
{

protected $guarded = [];

/**
* Create a new Eloquent model instance.
*
* @param array $attributes
*/
* Create a new Eloquent model instance.
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
$connection = config('easy_panel.database.connection') ?: config('database.default');
$table = config('easy_panel.database.panel_admin_table') ?: 'panel_admins';

$this->setConnection($connection);

$this->setTable($table);
$this->setConnection(config('easy_panel.database.connection'));
$this->setTable(config('easy_panel.database.panel_admin_table'));

parent::__construct($attributes);
}

protected $guarded = [];

public function user()
{
return $this->belongsTo(config('easy_panel.user_model'));
Expand Down

0 comments on commit 424a117

Please sign in to comment.