blog api created
This commit is contained in:
1 parent
4f101885a8
commit
d98bc7bbb0
27 files changed
+1022
-44
No files matched your search
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders\Blog;
|
||||
|
||||
use App\Models\Blog\Category;
|
||||
use App\Models\Blog\Post;
|
||||
use App\Models\Blog\Tag;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class BlogSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
// انتخاب کاربر با id = 1
|
||||
$user = User::find(1);
|
||||
|
||||
if (!$user) {
|
||||
// اگر کاربر با id 1 وجود نداشت، خطا ایجاد کن یا پیام مناسبی بده
|
||||
$this->command->error('User with id 1 not found.');
|
||||
return;
|
||||
}
|
||||
|
||||
// ایجاد دستهبندیها
|
||||
Category::factory(10)->create();
|
||||
|
||||
// ایجاد تگها
|
||||
Tag::factory(20)->create();
|
||||
|
||||
// ایجاد پستها و تخصیص به کاربر با id = 1
|
||||
Post::factory(50)->create([
|
||||
'author_id' => $user->id, // اختصاص پستها به کاربر id 1
|
||||
])->each(function ($post) {
|
||||
// اختصاص تگها به پست
|
||||
$tags = Tag::inRandomOrder()->take(rand(2, 5))->pluck('id');
|
||||
$post->tags()->attach($tags);
|
||||
|
||||
// ایجاد پستهای مرتبط
|
||||
$relatedPosts = Post::inRandomOrder()->where('id', '!=', $post->id)->take(rand(1, 3))->pluck('id');
|
||||
$post->relatedPosts()->attach($relatedPosts);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user