Debug School

rakesh kumar
rakesh kumar

Posted on

Laravel Error:SQLSTATE[HY000] [1049] Unknown database 'forge'

yesterday i run code i got errors
ERROR
SQLSTATE[HY000] [1049] Unknown database 'forge'

MY CODE
there is a function in controller
$get_taskboard = TaskBoardNotification::get();

  public function tokenmy($org_slug)

    {

        $get_auth_email = Auth::user()->email;
        Log::info('we are getting user profile user id' . 
    $get_auth_email);
        $getting_notification = Notification::orderBy('id', 
       'DESC')->take(8)->get();
        $notificationCount = 0;
        foreach ($getting_notification as $notification) {

            $data = json_decode($notification->data, true);

            if ($notification->read_at == null && $data['u_org_user_email'] == $get_auth_email) {

                $notificationCount++;

            }

        }

        Log::info("notificationCount count me hai abhi" . $notificationCount);

        Log::info('getting notification me kya aa rha hai' . $getting_notification);

        $get_taskboard = TaskBoardNotification::get();

}
Enter fullscreen mode Exit fullscreen mode

In Modal

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class TaskBoardNotification extends Model
{
    use HasFactory;

    protected $connection = 'mysqltaskboards';
    protected $table = 'notifications';

    protected $casts = [
        'id' => 'string'
    ];
}
Enter fullscreen mode Exit fullscreen mode

In database.php

   'mysqltaskboards' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST_TASKS_BOARD_COUNT', 'localhost'),
            'port' => env('DB_PORT_TASKS_BOARD_COUNT', '3306'),
            'database' => env('DB_DATABASE_TASKS_BOARD_COUNT', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],
Enter fullscreen mode Exit fullscreen mode

Solution
change in database.php inside config folder

  'database' => env('DB_DATABASE_TASKS_BOARD_COUNT', 'wz_tasks_board_ms'),
Enter fullscreen mode Exit fullscreen mode

Improved code

     'mysqltaskboards' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST_TASKS_BOARD_COUNT', 'localhost'),
            'port' => env('DB_PORT_TASKS_BOARD_COUNT', '3306'),
            'database' => env('DB_DATABASE_TASKS_BOARD_COUNT', 'wz_tasks_board_ms'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            // ...
        ],
Enter fullscreen mode Exit fullscreen mode

Top comments (0)