From cb38eaf76582e071397fb46492894773156a1c6a Mon Sep 17 00:00:00 2001 From: Gilles Felix Date: Wed, 17 Jan 2024 16:16:08 +0100 Subject: [PATCH] Fix views --- README.md | 105 ++++++++++++++---- resources/views/fields/description.blade.php | 3 + resources/views/fields/keywords.blade.php | 3 + .../views/fields/og_description.blade.php | 4 + resources/views/fields/og_image.blade.php | 4 + resources/views/fields/og_title.blade.php | 4 + resources/views/fields/robots.blade.php | 3 + resources/views/fields/title.blade.php | 3 + resources/views/meta.blade.php | 37 ++---- src/LaravelMetaServiceProvider.php | 2 +- 10 files changed, 117 insertions(+), 51 deletions(-) create mode 100644 resources/views/fields/description.blade.php create mode 100644 resources/views/fields/keywords.blade.php create mode 100644 resources/views/fields/og_description.blade.php create mode 100644 resources/views/fields/og_image.blade.php create mode 100644 resources/views/fields/og_title.blade.php create mode 100644 resources/views/fields/robots.blade.php create mode 100644 resources/views/fields/title.blade.php diff --git a/README.md b/README.md index 4a2642f..783a801 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,31 @@ -# Laravel Publishable +# Laravel Meta -[![Novius CI](https://github.com/novius/laravel-publishable/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/novius/laravel-publishable/actions/workflows/main.yml) +[![Novius CI](https://github.com/novius/laravel-meta/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/novius/laravel-meta/actions/workflows/main.yml) [![Packagist Release](https://img.shields.io/packagist/v/novius/laravel-nova-publishable.svg?maxAge=1800&style=flat-square)](https://packagist.org/packages/novius/laravel-nova-publishable) [![License: AGPL v3](https://img.shields.io/badge/License-AGPL%20v3-blue.svg)](http://www.gnu.org/licenses/agpl-3.0) ## Introduction -A package for making Laravel Eloquent models "publishable" using 4 states : draft, published, unpublished and scheduled. -Manage an additional `published_first_at` date for order by and display. +A package to manage meta fields on Laravel Eloquent models. ## Requirements -* Laravel 8.0, 9.0 or 10.0 +* Laravel 10.0 ## Installation You can install the package via composer: ```bash -composer require novius/laravel-publishable +composer require novius/laravel-meta ``` +Optionally you can also: + ```bash php artisan vendor:publish --provider="Novius\Publishable\LaravelPublishableServiceProvider" --tag=lang +php artisan vendor:publish --provider="Novius\Publishable\LaravelPublishableServiceProvider" --tag=views ``` ## Usage @@ -36,7 +38,7 @@ Schema::create('posts', function (Blueprint $table) { $table->string('title'); $table->text('text'); $table->timestamps(); - $table->publishable(); // Macro provided by the package + $table->addMeta(); // Macro provided by the package }); ``` @@ -46,31 +48,94 @@ Schema::create('posts', function (Blueprint $table) { namespace App\Models; use \Illuminate\Database\Eloquent\Model; -use \Novius\LaravelMeta\Publishable; +use Novius\LaravelMeta\Traits\HasMeta; class Post extends Model { - use Publishable; + use HasMeta; ... } ``` -#### Extensions +You can also add this method which will define the default operation of the trait -The extensions shipped with this trait include; `notPublished`, `published`, `onlyDrafted`, `onlyExpired`, `onlyWillBePublished` and can be used accordingly: +```php + + public function hasMetaConfig(): ModelConfig + { + if (! isset($this->hasMetaConfig)) { + $this->hasMetaConfig = new ModelConfig( + IndexFollow::index_follow, // The default value of the seo_robots field if not defined + 'title', // The name of field for the default value of the seo_title and og_title fields if not defined. Can also be a callable, see below + function($model) { // The default value of the seo_description and og_description fields if not defined. Can also be a string, see above + return $model->description; + } + ); + } + + return $this->hasMetaConfig; + } +``` + +#### Extensions ```php $post = Post::first(); -$post->isPublished(); - -$postsPublished = Post::all(); -$postsPublished = Post::query()->published(); -$onlyNotPublishedPosts = Post::query()->notPublished(); -$onlyDraftedPosts = Post::query()->onlyDrafted(); -$onlyExpiredPosts = Post::query()->onlyExpired(); -$onlyWillBePublishedPosts = Post::query()->onlyWillBePublished(); +$post->canBeIndexedByRobots(); +$post->seo_robots; +$post->seo_title; +$post->seo_description; +$post->seo_keywords; +$post->og_title; +$post->og_description; +$post->og_image; + +$postsIndexableByRobots = Post::query()->indexableByRobots(); +$postsNotIndexableByRobots = Post::query()->notIndexableByRobots(); ``` -When not specifing any additional scopes, all not published models are excluded from the query by default to prevent leaks of not published data. +#### Nova + +If you use Laravel Nova, you can do that on your Resource on a Model using HasMeta : + +```php +sortable(), + + new Panel('Model', [ + Text::make('Title', 'title'), + Textarea::make('Description', 'description'), + ]), + new Panel('Meta', $this->getSEONovaFields([ + 'seo_keywords' => false, // This will not display field for seo_keywords + 'required' => [ + 'seo_robots' => true, // This will set required for field seo_robots + ], + ])), + ]; + } +} + +``` + +#### Front + + + ### Testing diff --git a/resources/views/fields/description.blade.php b/resources/views/fields/description.blade.php new file mode 100644 index 0000000..17870fe --- /dev/null +++ b/resources/views/fields/description.blade.php @@ -0,0 +1,3 @@ +@if (!empty($description)) + +@endif diff --git a/resources/views/fields/keywords.blade.php b/resources/views/fields/keywords.blade.php new file mode 100644 index 0000000..7ef58a7 --- /dev/null +++ b/resources/views/fields/keywords.blade.php @@ -0,0 +1,3 @@ +@if (!empty($keywords)) + +@endif diff --git a/resources/views/fields/og_description.blade.php b/resources/views/fields/og_description.blade.php new file mode 100644 index 0000000..4769fc5 --- /dev/null +++ b/resources/views/fields/og_description.blade.php @@ -0,0 +1,4 @@ +@if(!empty($og_description)) + + +@endif diff --git a/resources/views/fields/og_image.blade.php b/resources/views/fields/og_image.blade.php new file mode 100644 index 0000000..8e4b391 --- /dev/null +++ b/resources/views/fields/og_image.blade.php @@ -0,0 +1,4 @@ +@if (!empty($og_image)) + + +@endif diff --git a/resources/views/fields/og_title.blade.php b/resources/views/fields/og_title.blade.php new file mode 100644 index 0000000..dab5afe --- /dev/null +++ b/resources/views/fields/og_title.blade.php @@ -0,0 +1,4 @@ +@if(!empty($og_title)) + + +@endif diff --git a/resources/views/fields/robots.blade.php b/resources/views/fields/robots.blade.php new file mode 100644 index 0000000..efcc46b --- /dev/null +++ b/resources/views/fields/robots.blade.php @@ -0,0 +1,3 @@ +@if (!empty($robots)) + +@endif diff --git a/resources/views/fields/title.blade.php b/resources/views/fields/title.blade.php new file mode 100644 index 0000000..fd48192 --- /dev/null +++ b/resources/views/fields/title.blade.php @@ -0,0 +1,3 @@ +@if (!empty($title)) + {{ $title }} +@endif diff --git a/resources/views/meta.blade.php b/resources/views/meta.blade.php index a5dc52b..e07b241 100644 --- a/resources/views/meta.blade.php +++ b/resources/views/meta.blade.php @@ -1,32 +1,9 @@ @props(['title', 'description', 'robots', 'keywords', 'og_title', 'og_description', 'og_image']) -@if (!empty($robots)) - -@endif - -@if (!empty($title)) - {{ $title }} -@endif - -@if (!empty($description)) - -@endif - -@if (!empty($keywords)) - -@endif - -@if(!empty($og_title)) - - -@endif - -@if(!empty($og_description)) - - -@endif - -@if (!empty($og_image)) - - -@endif +@include('$laravel-meta::fields.robots') +@include('$laravel-meta::fields.title') +@include('$laravel-meta::fields.description') +@include('$laravel-meta::fields.keywords') +@include('$laravel-meta::fields.og_title') +@include('$laravel-meta::fields.og_description') +@include('$laravel-meta::fields.og_image') diff --git a/src/LaravelMetaServiceProvider.php b/src/LaravelMetaServiceProvider.php index 3c55d27..41da72c 100644 --- a/src/LaravelMetaServiceProvider.php +++ b/src/LaravelMetaServiceProvider.php @@ -17,7 +17,7 @@ public function register() public function boot() { - $this->publishes([__DIR__.'/resources/views' => resource_path('views/vendor/laravel-meta')]); + $this->publishes([__DIR__.'/../resources/views' => resource_path('views/vendor/laravel-meta')]); $this->publishes([__DIR__.'/../lang' => $this->app->langPath('vendor/laravel-meta')]); $this->loadViewsFrom(__DIR__.'/resources/views', 'laravel-meta');