1. Go to this page and download the library: Download novius/laravel-meta library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
novius / laravel-meta example snippets
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('text');
$table->timestamps();
$table->addMeta(); // Macro provided by the package
});
namespace App\Models;
use \Illuminate\Database\Eloquent\Model;
use Novius\LaravelMeta\Traits\HasMeta;
class Post extends Model {
use HasMeta;
...
}
public function getMetaConfig(): MetaModelConfig
{
if (! isset($this->metaConfig)) {
$this->metaConfig = MetaModelConfig::make()
->setDefaultSeoRobots(IndexFollow::index_follow) // The default value of the seo_robots field if not defined
->setFallbackTitle('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
->setFallbackDescription(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;
})
->setFallbackImage('picture')
->setCallbackOgImageUrl(function($model) { // The function to get the og_image url
if ($model->og_image) {
return asset('storage/'.$model->og_image);
}
return null;
})
->setOgImageDisk('a_disk')
->setOgImagePath('/og-image/');
}
return $this->metaConfig;
}
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' => null, // This will not display field for seo_keywords
'og_image' => AlternativeImageField::make(trans('laravel-meta::messages.og_image'), $columnMeta.'->og_image')
->hideFromIndex(),
])->toArray()),
];
}
}
use Novius\LaravelMeta\Facades\CurrentModel;
class HasModelController extends Controller
{
public function show($id)
{
$model = HasMetaModel::find($id);
CurrentModel::setModel($model);
return view('has-meta', compact('model'));
}
}