|
| 1 | +# Meta Eloquent |
| 2 | + |
| 3 | +Meta Eloquent is an eloquent booster that allows it to easily manage HTTP Meta tags. |
| 4 | + |
| 5 | +# Installation |
| 6 | + |
| 7 | +## 1. Dependecy |
| 8 | + |
| 9 | +Using composer, execute the following command to automatically update your composer.json: |
| 10 | + |
| 11 | +``` |
| 12 | +composer require dishark/metaeloquent |
| 13 | +``` |
| 14 | +or manually update your composer.json file |
| 15 | + |
| 16 | +``` |
| 17 | +{ |
| 18 | + "require": { |
| 19 | + "dishark/metaeloquent": "dev-master" |
| 20 | + } |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +## 2. Provider |
| 25 | + |
| 26 | +You need to update your application configuration in order to register the package, so it can be loaded by Laravel. Just update your config/app.php file adding the following code at the end of your 'providers' section: |
| 27 | + |
| 28 | +```php |
| 29 | +// file START omitted |
| 30 | + 'providers' => [ |
| 31 | + // other providers omitted |
| 32 | + 'Dishark\Metaeloquent\MetaEloquentServiceProvider', |
| 33 | + ], |
| 34 | +// file END omitted |
| 35 | +``` |
| 36 | + |
| 37 | +#Usage |
| 38 | + |
| 39 | +Make sure to use `Dishark\Metaeloquent\Traits\MetaTrait` in your model. Then declare the `$metaAttributes` property specifying the metatag as the key and the column as the value. |
| 40 | + |
| 41 | +Example 1: |
| 42 | + |
| 43 | +````php |
| 44 | +<?php App\Post; |
| 45 | + |
| 46 | +use Dishark\Metaeloquent\Traits\MetaTrait; |
| 47 | + |
| 48 | +class Post { |
| 49 | + use MetaTrait; |
| 50 | + |
| 51 | + protected $metaAttributes = [ |
| 52 | + 'author' => 'author', |
| 53 | + 'description' => 'title', |
| 54 | + 'keywords' => 'keywords', |
| 55 | + ]; |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +Sometimes the column isn't enough. Let's create some Meta accessor: |
| 60 | + |
| 61 | +Example 2: |
| 62 | + |
| 63 | +```php |
| 64 | +<?php App\Post; |
| 65 | + |
| 66 | +use Dishark\Metaeloquent\Traits\MetaTrait; |
| 67 | + |
| 68 | +class Post { |
| 69 | + use MetaTrait; |
| 70 | + |
| 71 | + protected $metaAttributes = [ |
| 72 | + 'author' => 'author', |
| 73 | + 'description' => 'title', |
| 74 | + 'keywords' => 'keywords', |
| 75 | + ]; |
| 76 | + |
| 77 | + public function getMetaAuthor() |
| 78 | + { |
| 79 | + return $this->author->name; |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +## View |
| 85 | + |
| 86 | +In your view: |
| 87 | + |
| 88 | +```php |
| 89 | +@extends ('layout') |
| 90 | + |
| 91 | +@section ('metadata') |
| 92 | +{!! $post->meta() !!} |
| 93 | +@endsection |
| 94 | +``` |
| 95 | + |
| 96 | +The layout: |
| 97 | + |
| 98 | +```php |
| 99 | +<head> |
| 100 | +@yield('metadata') |
| 101 | +</head> |
| 102 | +``` |
| 103 | + |
| 104 | +So now the author attribute will be called through this method instead. |
| 105 | + |
| 106 | +## OpenGraph |
| 107 | + |
| 108 | +```php |
| 109 | +<?php App\Post; |
| 110 | + |
| 111 | +use Dishark\Metaeloquent\Traits\MetaTrait; |
| 112 | + |
| 113 | +class Post { |
| 114 | + use MetaTrait; |
| 115 | + |
| 116 | + protected $metaAttributes = [ |
| 117 | + 'author' => 'author', |
| 118 | + 'description' => 'title', |
| 119 | + 'keywords' => 'keywords', |
| 120 | + |
| 121 | + 'og:title' => 'name', |
| 122 | + 'og:image' => 'image', |
| 123 | + 'og:type' => 'article', |
| 124 | + 'og:url' => 'url' |
| 125 | + ]; |
| 126 | + |
| 127 | + public function getMetaImage() |
| 128 | + { |
| 129 | + return asset('images_path/' . $this->image); |
| 130 | + } |
| 131 | + |
| 132 | + public function getMetaUrl() |
| 133 | + { |
| 134 | + return route('articles', $this->slug); |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
0 commit comments