laravel-learn-bbs/app/Jobs/TranslateSlug.php
2018-01-02 01:37:18 +08:00

44 lines
1.0 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\Topic;
use App\Tools\SlugTranslateHandler;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class TranslateSlug implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $topic;
/**
* Create a new job instance.
* @param $topic
* @return void
*/
public function __construct(Topic $topic)
{
$this->topic = $topic;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// 请求百度 API 接口进行翻译
$slug = app(SlugTranslateHandler::class)->translate($this->topic->title);
// 为了避免模型监控器死循环调用,我们使用 DB 类直接对数据库进行操作
\DB::table('topics')->where('id', $this->topic->id)->update(['slug' => $slug]);
}
}