0

I have set up a migration file:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class LogggingTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::connection('mysql2')->create('webServiceLogs', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->timestamps();
        $table->string('query');
        $table->string('response');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('webServiceLogs');
}
}

And a model:

namespace App\Models;

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

class WebServiceLog extends Model
{
    use HasFactory;

    protected $table = 'webServiceLogs';

}

Here is the controller function that saves the data:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\WebServiceLog;
use Illuminate\Foundation\Testing\RefreshDatabase;

class LoggingController extends Controller
{
/**
 * Log WS requests.
 *
 * @return \Illuminate\Http\Response
 */
public function LogWebService($url, $data)
{
    $web_service_log = new WebServiceLog();

    $web_service_log->query    = json_encode($url, true);
    $web_service_log->response = json_encode($data ,true);

    $web_service_log->save();
}
}

When I migrate the table is created in phpmyadmin but When I run the 'LogWebService' function in my form I get this error, even though the table exists in phpmyadmin:

SQLSTATE[HY000]: General error: 1 no such table: webServiceLogs

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
JED BOYLE
  • 85
  • 6
  • 1
    Change the name of the migration from LoggingTable to CreateWebServiceLogsTable, since this follows the standard naming of migration for laravel and this might be causing your issue. Also besides that the standard naming for tables is not camel case so I recommend renaming the migration to web_service_logs. – dz0nika Sep 14 '22 at 12:32
  • Does this answer your question? [How to use multiple databases in Laravel](https://stackoverflow.com/questions/31847054/how-to-use-multiple-databases-in-laravel) – Abdulla Nilam Sep 20 '22 at 12:43

2 Answers2

1

On your migration you change the connection, you are just missing that in your model

WebServiceLog extends Model
{
    use HasFactory;

    protected $connection= 'mysql2';

    protected $table = 'webServiceLogs';
}
josezenem
  • 300
  • 1
  • 5
-1

RESOLVED:

I am using multiple databaseses (sqlite and mysql). In database.php I updated:

'default' => env('DB_CONNECTION', 'mysql'),

to:

'default' => 'mysql2',
JED BOYLE
  • 85
  • 6