fulinqingjie/database/seeders/ArticleSeeder.php

62 lines
1.7 KiB
PHP

<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Throwable;
class ArticleSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//老文章分类表
$oldArticles = DB::connection('old_mysql')->table('cases')->get();
$categoryArr = [
0=> 'examples',
1=> 'services',
2=> 'news',
];
foreach($oldArticles as $article){
//处理富文本内容图片地址;
$_content = null;
if($article->content){
$_content = str_replace('http://www.gxflqj.cn/', 'http://www.gxflqj.cn/storage', $article->content);
}
$_article = [
'id' => $article -> id,
'category' => $categoryArr[$article -> type ?? 0],
'title' => $article->title,
'cover' => $article->img,
'content' => $_content,
'published_at' => now(),
'sort' => $article -> sort ?? 0,
'is_recommend' => $article->recommend,
'is_enable' => 1,
'created_at' => now(),
'updated_at' => now(),
];
$articles[] = $_article;
}
if(count($articles) > 0){
DB::table('articles')->truncate();
try {
DB::begintransaction();
DB::table('articles')->insert($articles);
DB::commit();
} catch (Throwable $th) {
DB::rollBack();
report($th);
}
}
}
}