article seeder
parent
6ecdc00dec
commit
558eedbd3d
|
|
@ -3,12 +3,12 @@
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
|
||||||
class Article extends Model
|
class Article extends Model
|
||||||
{
|
{
|
||||||
use HasDateTimeFormatter;
|
use HasDateTimeFormatter, HasFactory;
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'is_show' => 'boolean',
|
'is_show' => 'boolean',
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
use App\Models\Article;
|
||||||
|
|
||||||
|
class ArticleFactory extends Factory
|
||||||
|
{
|
||||||
|
protected $model = Article::class;
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function definition()
|
||||||
|
{
|
||||||
|
$images = [
|
||||||
|
'https://qiniu.abcdefg.fun/banner.png',
|
||||||
|
'https://qiniu.abcdefg.fun/banner1.png',
|
||||||
|
'https://qiniu.abcdefg.fun/banner2.png',
|
||||||
|
'https://qiniu.abcdefg.fun/banner3.png',
|
||||||
|
'https://qiniu.abcdefg.fun/banner/banner4.png',
|
||||||
|
'https://qiniu.abcdefg.fun/banner/banner5.png',
|
||||||
|
'https://qiniu.abcdefg.fun/banner/banner6.png',
|
||||||
|
'https://qiniu.abcdefg.fun/banner/banner7.png',
|
||||||
|
'https://qiniu.abcdefg.fun/banner/banner8.png',
|
||||||
|
];
|
||||||
|
$faker = $this->faker;
|
||||||
|
return [
|
||||||
|
'title' => $faker->sentence(5, true),
|
||||||
|
'author_name' => $faker->firstNameMale,
|
||||||
|
'subtitle' => $faker->sentence(5, true),
|
||||||
|
'cover' => $faker->randomElement($images),
|
||||||
|
'content' => $faker->randomHtml(2, 3),
|
||||||
|
'is_show' => 1,
|
||||||
|
'is_recommend' => 1,
|
||||||
|
'sort' => 1,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use App\Models\{Article, ArticleCategory};
|
||||||
|
|
||||||
|
class ArticleSeeder extends Seeder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the database seeds.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
Article::truncate();
|
||||||
|
ArticleCategory::truncate();
|
||||||
|
$categoris = [
|
||||||
|
['name' => '自然', 'is_show' => 1, 'is_recommend' => 1],
|
||||||
|
['name' => '文化', 'is_show' => 1, 'is_recommend' => 1],
|
||||||
|
['name' => '生活', 'is_show' => 1, 'is_recommend' => 1],
|
||||||
|
];
|
||||||
|
foreach($categoris as $item) {
|
||||||
|
$category = ArticleCategory::create($item);
|
||||||
|
Article::factory()->count(10)->create(['category_id' => $category->id]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue