PHP code example of novius / laravel-meta

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;
    }

$model = ModelHasMeta::first();
$model->canBeIndexedByRobots();
$model->seo_robots
$model->seo_title
$model->seo_description
$model->seo_keywords
$model->og_type
$model->og_title
$model->og_description
$model->og_image
$model->og_image_url

$indexableByRobots = Post::query()->indexableByRobots();
$notIndexableByRobots = Post::query()->notIndexableByRobots();



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'));
    }
}

@section('metas')
    @ayout>
    <div class="container mx-auto px-4 md:px-0">
        <h1>{{ $model->title }}</h1>
        <p>{{ $model->description }}</p>
    </div>
</x-main-layout>
bash
php artisan vendor:publish --provider="Novius\LaravelMeta\LaravelMetaServiceProvider" --tag=lang
php artisan vendor:publish --provider="Novius\LaravelMeta\LaravelMetaServiceProvider" --tag=views