Home > OS >  Laravel: Getting 'SQLSTATE[HY000]: General error: 1 no such table: webServiceLogs' when tr
Laravel: Getting 'SQLSTATE[HY000]: General error: 1 no such table: webServiceLogs' when tr

Time:09-14

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

CodePudding user response:

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';

}

CodePudding user response:

RESOLVED:

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

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

to:

'default' => 'mysql2',
  • Related