Database Connection
Here is an overview of the steps needed to customize the database connection used for the workflow v2 durable models. This is only required if you want to use a different database connection than the default connection you specified for your Laravel application.
- Create classes in your app models directory that extend the base v2 model classes
- Set the desired
$connectionoption in each class - Publish the workflow config file
- Update the
workflows.v2model bindings to point at your custom classes
Extending V2 Workflow Models
In app\Models\V2\WorkflowInstance.php put this.
namespace App\Models\V2;
use Workflow\V2\Models\WorkflowInstance as BaseWorkflowInstance;
class WorkflowInstance extends BaseWorkflowInstance
{
protected $connection = 'mysql';
}
Repeat the pattern for each durable v2 model you need to re-route — at minimum:
Workflow\V2\Models\WorkflowRunWorkflow\V2\Models\WorkflowHistoryEventWorkflow\V2\Models\WorkflowTaskWorkflow\V2\Models\WorkflowCommandWorkflow\V2\Models\WorkflowLinkWorkflow\V2\Models\ActivityExecutionWorkflow\V2\Models\ActivityAttemptWorkflow\V2\Models\WorkflowTimerWorkflow\V2\Models\WorkflowFailureWorkflow\V2\Models\WorkflowRunSummaryWorkflow\V2\Models\WorkflowScheduleWorkflow\V2\Models\WorkflowScheduleHistoryEvent
Each subclass should declare protected $connection = 'mysql'; (or whichever connection name you defined in config/database.php).
Registering Custom Models
Publish the workflow config file and update the workflows.v2.*_model bindings to point at your custom classes:
// config/workflows.php
'v2' => [
'instance_model' => App\Models\V2\WorkflowInstance::class,
'run_model' => App\Models\V2\WorkflowRun::class,
'history_event_model' => App\Models\V2\WorkflowHistoryEvent::class,
'task_model' => App\Models\V2\WorkflowTask::class,
'command_model' => App\Models\V2\WorkflowCommand::class,
// ...
],
The package resolves models through Workflow\V2\Support\ConfiguredV2Models, so any relation or query that hits a v2 model will transparently pick up the configured connection.
Migrations
Workflow migrations are auto-loaded from the package (WorkflowServiceProvider::loadMigrationsFrom) and run against the default database connection. When you route the v2 tables at a non-default connection, publish the migrations with php artisan vendor:publish --tag=migrations and set protected $connection = 'mysql'; on each published migration class so php artisan migrate runs them against the correct database.