diff --git a/database/migrations/create_seo_table.php.stub b/database/migrations/create_seo_table.php.stub index 08d8c17..c0f8dd4 100644 --- a/database/migrations/create_seo_table.php.stub +++ b/database/migrations/create_seo_table.php.stub @@ -18,6 +18,7 @@ return new class extends Migration $table->string('image')->nullable(); $table->string('author')->nullable(); $table->string('robots')->nullable(); + $table->string('canonical_url')->nullable(); $table->timestamps(); }); diff --git a/src/Models/SEO.php b/src/Models/SEO.php index 75f526e..23e26c2 100644 --- a/src/Models/SEO.php +++ b/src/Models/SEO.php @@ -20,6 +20,7 @@ public function model(): MorphTo public function prepareForUsage(): SEOData { if ( method_exists($this->model, 'getDynamicSEOData') ) { + /** @var SEOData $overrides */ $overrides = $this->model->getDynamicSEOData(); } @@ -45,6 +46,7 @@ public function prepareForUsage(): SEOData type: $overrides->type ?? null, locale: $overrides->locale ?? null, robots: $overrides->robots ?? $this->robots, + canonical_url: $overrides->canonical_url ?? $this->canonical_url, ); } } \ No newline at end of file diff --git a/src/Support/SEOData.php b/src/Support/SEOData.php index c88dfc7..f0121bf 100644 --- a/src/Support/SEOData.php +++ b/src/Support/SEOData.php @@ -31,6 +31,7 @@ public function __construct( public ?string $favicon = null, public ?string $locale = null, public ?string $robots = null, + public ?string $canonical_url = null, ) { if ( $this->locale === null ) { $this->locale = Str::of(app()->getLocale())->lower()->kebab(); diff --git a/src/Tags/CanonicalTag.php b/src/Tags/CanonicalTag.php index 10771d9..82c00bb 100644 --- a/src/Tags/CanonicalTag.php +++ b/src/Tags/CanonicalTag.php @@ -17,7 +17,7 @@ public static function initialize(SEOData $SEOData = null): static $collection = new static(); if ( config('seo.canonical_link') ) { - $collection->push(new LinkTag('canonical', $SEOData->url)); + $collection->push(new LinkTag('canonical', $SEOData->canonical_url ?? $SEOData->url)); } return $collection; diff --git a/tests/Feature/Tags/CanonicalTagTest.php b/tests/Feature/Tags/CanonicalTagTest.php index 3f9a0ff..3324e1f 100644 --- a/tests/Feature/Tags/CanonicalTagTest.php +++ b/tests/Feature/Tags/CanonicalTagTest.php @@ -1,6 +1,9 @@ assertDontSee('rel="canonical"', false); }); +it('can display the model level canonical url if set in database', function () { + config()->set('seo.canonical_link', true); + + $page = Page::create(); + + $page->seo->update([ + 'canonical_url' => 'https://example.com/canonical/url/test', + ]); + + $page->refresh(); + + get(route('seo.test-page', ['page' => $page])) + ->assertSee('', false); +}); + +it('can display the model level canonical url if set on override', function () { + config()->set('seo.canonical_link', true); + + $page = Page::create(); + + $page::$overrides = [ + 'canonical_url' => 'https://example.com/canonical/url/test', + ]; + + $page->refresh(); + + get(route('seo.test-page', ['page' => $page])) + ->assertSee('', false); +}); + +it('will not break if no canonical_url column exists in seo table', function () { + // New seo.canonical_url column was added in https://github.com/ralphjsmit/laravel-seo/pull/35. + config()->set('seo.canonical_link', true); + + $page = Page::create(); + + expect(Schema::hasColumn('seo', 'canonical_url')) + ->toBeTrue(); + + Schema::table('seo', function (Blueprint $table) { + $table->dropColumn('canonical_url'); + }); + + expect(Schema::hasColumn('seo', 'canonical_url')) + ->toBeFalse(); + + $page->refresh(); + + get(route('seo.test-page', ['page' => $page])) + ->assertOk(); +}); \ No newline at end of file