Skip to content

Commit

Permalink
Merge pull request #104 from krls2020/patch-1
Browse files Browse the repository at this point in the history
Fix migration
  • Loading branch information
pxlrbt authored Oct 22, 2024
2 parents baa1df4 + 6ad8753 commit 76f0315
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions database/migrations/2022_06_09_155042_create_addressable_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ public function up()
$table->string('state')->nullable();
$table->string('zip')->nullable();

$table->string('full_address')->virtualAs(
config('database.default') === 'sqlite'
? "street || ', ' || zip || ' ' || city"
: "CONCAT(street, ', ', zip, ' ', city)"
);
// Determine database type and use appropriate syntax for generated columns
if (DB::getDriverName() === 'pgsql') {
// PostgreSQL requires stored columns with || for concatenation
$table->string('full_address')->storedAs("street || ', ' || zip || ' ' || city");
} elseif (DB::getDriverName() === 'sqlite') {
// SQLite uses || for concatenation, virtualAs is used
$table->string('full_address')->virtualAs("street || ', ' || zip || ' ' || city");
} else {
// MySQL uses CONCAT for string concatenation
$table->string('full_address')->virtualAs("CONCAT(street, ', ', zip, ' ', city)");
}

$table->timestamps();
});
Expand Down

0 comments on commit 76f0315

Please sign in to comment.