Setup Meta Tag save in background - digitaldreams/laravel-seo-tools GitHub Wiki
If you do not like to make your controller dirty then this will be a good solution for you. First of all look at this
// config/seo.php
'models' => [
\App\Models\Post::class => [
'route' => ['posts.store','posts.update'],
'job' => \App\Jobs\SavePostSeoTagsJob::class
]
]
This will register Post model. So whenever Post model Created or Updated then meta tags from Form will be saved automatically and you don't need to add any code inside controller. So its save to remove this package anytime without changing into your code.
How it works!
Here route key are for protecting when to call SavePostSeoTagsJob Class. For example we only want to save meta tags when a post is created or updated inside store and update method of PostController. You may create post from Faker or somewhere else for testing purpose and that will not execute job class.
Here job key takes a full Laravel Job class. You can create job class like
php artisan make:job SavePostSeoTagsJob
First parameter of this class Constructor will be the Post model Object that is just created or saved. Then on handle method run following code .
Seo::save($this->post, route('posts.show', $this->post->id), [
'title' => $this->post->title,
])
Here is the full class.
<?php
namespace App\Jobs;
use App\Models\Post;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use SEO\Seo;
class SavePostSeoTagsJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* @var Post
*/
protected $post;
/**
* Create a new job instance.
*
* @param Post $post
*/
public function __construct(Post $post)
{
$this->post = $post;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Seo::save($this->post, route('posts.show', $this->post->id), [
'title' => $this->post->title,
]);
}
}