Skip to content

Commit

Permalink
Fix views
Browse files Browse the repository at this point in the history
  • Loading branch information
felixgilles committed Jan 17, 2024
1 parent 07cced9 commit cb38eaf
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 51 deletions.
105 changes: 85 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
});
```

Expand All @@ -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
<?php

use Novius\LaravelMeta\Traits\NovaResourceHasMeta;

class HasMetaModel extends Resource
{
use NovaResourceHasMeta;

/**
* Get the fields displayed by the resource.
*
* @return array
*/
public function fields(NovaRequest $request)
{
return [
ID::make()->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

Expand Down
3 changes: 3 additions & 0 deletions resources/views/fields/description.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@if (!empty($description))
<meta name="description" content="{{ $description }}">
@endif
3 changes: 3 additions & 0 deletions resources/views/fields/keywords.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@if (!empty($keywords))
<meta name="keywords" content="{{ $keywords }}">
@endif
4 changes: 4 additions & 0 deletions resources/views/fields/og_description.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@if(!empty($og_description))
<meta property="og:description" content="{{ $og_description }}" />
<meta name="twitter:description" content="{{ $og_description }}" />
@endif
4 changes: 4 additions & 0 deletions resources/views/fields/og_image.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@if (!empty($og_image))
<meta property="og:image" content="{{ asset('storage/' . $og_image) }}" />
<meta name="twitter:image" content="{{ asset('storage/' . $og_image) }}" />
@endif
4 changes: 4 additions & 0 deletions resources/views/fields/og_title.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@if(!empty($og_title))
<meta property="og:title" content="{{ $og_title }}" />
<meta name="twitter:title" content="{{ $og_title }}" />
@endif
3 changes: 3 additions & 0 deletions resources/views/fields/robots.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@if (!empty($robots))
<meta name="robots" content="{{ $robots }}">
@endif
3 changes: 3 additions & 0 deletions resources/views/fields/title.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@if (!empty($title))
<title>{{ $title }}</title>
@endif
37 changes: 7 additions & 30 deletions resources/views/meta.blade.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
@props(['title', 'description', 'robots', 'keywords', 'og_title', 'og_description', 'og_image'])

@if (!empty($robots))
<meta name="robots" content="{{ $robots }}">
@endif

@if (!empty($title))
<title>{{ $title }}</title>
@endif

@if (!empty($description))
<meta name="description" content="{{ $description }}">
@endif

@if (!empty($keywords))
<meta name="keywords" content="{{ $keywords }}">
@endif

@if(!empty($og_title))
<meta property="og:title" content="{{ $og_title }}" />
<meta name="twitter:title" content="{{ $og_title }}" />
@endif

@if(!empty($og_description))
<meta property="og:description" content="{{ $og_description }}" />
<meta name="twitter:description" content="{{ $og_description }}" />
@endif

@if (!empty($og_image))
<meta property="og:image" content="{{ asset('storage/' . $og_image) }}" />
<meta name="twitter:image" content="{{ asset('storage/' . $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')
2 changes: 1 addition & 1 deletion src/LaravelMetaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit cb38eaf

Please sign in to comment.