skip to Main Content

I have set up a migration file:

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

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

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class WebServiceLog extends Model
{
    use HasFactory;

    protected $table = 'webServiceLogs';

}

Here is the controller function that saves the data:

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppModelsWebServiceLog;
use IlluminateFoundationTestingRefreshDatabase;

class LoggingController extends Controller
{
/**
 * Log WS requests.
 *
 * @return IlluminateHttpResponse
 */
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

2

Answers


  1. Chosen as BEST ANSWER

    RESOLVED:

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

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

    to:

    'default' => 'mysql2',
    

  2. 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';
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search