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
RESOLVED:
I am using multiple databaseses (sqlite and mysql). In database.php I updated:
to:
On your migration you change the connection, you are just missing that in your model