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,29 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories\Blog;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Category>
|
||||
*/
|
||||
class CategoryFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$name = $this->faker->unique()->word;
|
||||
return [
|
||||
'name' => ucfirst($name),
|
||||
'slug' => Str::slug($name),
|
||||
'description' => $this->faker->paragraph,
|
||||
'short_description' => $this->faker->sentence,
|
||||
'image' => $this->faker->imageUrl(640, 480, 'business', true),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories\Blog;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use App\Models\Blog\Post;
|
||||
use App\Models\Blog\Category;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Tag>
|
||||
*/
|
||||
class PostFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
|
||||
protected $model = Post::class;
|
||||
|
||||
|
||||
public function definition(): array
|
||||
{
|
||||
$title = $this->faker->sentence;
|
||||
return [
|
||||
'title' => $title,
|
||||
'body' => $this->faker->paragraphs(5, true),
|
||||
'excerpt' => $this->faker->text(200),
|
||||
'slug' => Str::slug($title) . '-' . $this->faker->unique()->numberBetween(1, 1000),
|
||||
'author_id' => User::inRandomOrder()->first()->id ?? User::factory(),
|
||||
'category_id' => Category::inRandomOrder()->first()->id ?? Category::factory(),
|
||||
'featured_image' => $this->faker->imageUrl(800, 600, 'technics', true),
|
||||
'views' => $this->faker->numberBetween(0, 1000),
|
||||
'likes' => $this->faker->numberBetween(0, 500),
|
||||
'published_at' => $this->faker->dateTimeBetween('-1 years', 'now'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories\Blog;
|
||||
|
||||
use App\Models\Blog\Tag;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Tag>
|
||||
*/
|
||||
class TagFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
|
||||
protected $model = Tag::class;
|
||||
|
||||
public function definition(): array
|
||||
{
|
||||
$name = $this->faker->unique()->word;
|
||||
return [
|
||||
'name' => ucfirst($name),
|
||||
'slug' => Str::slug($name),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@ class UserFactory extends Factory
|
||||
*/
|
||||
public function unverified(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
return $this->state(fn(array $attributes) => [
|
||||
'email_verified_at' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('categories', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name')->unique();
|
||||
$table->string('slug')->unique();
|
||||
$table->text('description');
|
||||
$table->text('short_description')->nullable();
|
||||
$table->string('image')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('categories');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('tags', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name')->unique();
|
||||
$table->string('slug')->unique();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('tags');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('posts', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('title');
|
||||
$table->text('body');
|
||||
$table->text('excerpt'); // توضیحات مختصر
|
||||
$table->string('slug')->unique();
|
||||
$table->unsignedBigInteger('author_id'); // نویسنده
|
||||
$table->foreign('author_id')->references('id')->on('users')->onDelete('cascade');
|
||||
$table->unsignedBigInteger('category_id'); // دسته بندی
|
||||
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
|
||||
$table->string('featured_image')->nullable(); // تصویر شاخص
|
||||
$table->integer('views')->default(0); // تعداد بازدید
|
||||
$table->integer('likes')->default(0); // تعداد لایک
|
||||
$table->timestamp('published_at')->nullable(); // تاریخ انتشار
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('posts');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('post_tag', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('post_id');
|
||||
$table->unsignedBigInteger('tag_id');
|
||||
$table->timestamps();
|
||||
|
||||
// ایجاد کلیدهای خارجی
|
||||
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
|
||||
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
|
||||
|
||||
// جلوگیری از ورود تکراری
|
||||
$table->unique(['post_id', 'tag_id']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('post_tag');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('related_posts', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('post_id');
|
||||
$table->unsignedBigInteger('related_post_id');
|
||||
$table->timestamps();
|
||||
|
||||
// ایجاد کلیدهای خارجی
|
||||
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
|
||||
$table->foreign('related_post_id')->references('id')->on('posts')->onDelete('cascade');
|
||||
|
||||
// جلوگیری از ورود تکراری
|
||||
$table->unique(['post_id', 'related_post_id']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('related_posts');
|
||||
}
|
||||
};
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\AboutMe;
|
||||
use App\Models\Skill;
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
@@ -15,44 +16,53 @@ class AboutMeSeeder extends Seeder
|
||||
{
|
||||
// ایجاد رکورد AboutMe
|
||||
$aboutMe = AboutMe::create([
|
||||
'title' => 'About Me Title',
|
||||
'description' => 'This is a brief description about me.',
|
||||
'title' => 'About Me',
|
||||
'description' => 'I am a web developer with over 10 years of experience in creating dynamic and user-centric web applications. Specializing in React and Next.js, I build scalable solutions that enhance performance and user experience. I enjoy tackling complex challenges, from improving legacy systems to developing new applications. My focus on frontend development and seamless backend integration allows me to deliver innovative results while staying current with industry trends.',
|
||||
'image' => '/images/profile.png',
|
||||
]);
|
||||
|
||||
// ایجاد رکوردهای Skill
|
||||
Skill::create([
|
||||
'title' => 'FrontEnd',
|
||||
'title' => 'HTML & CSS (SASS, Responsive Design)',
|
||||
'image' => '/images/frontend.png',
|
||||
'description' => 'UI design description.',
|
||||
'description' => 'HTML & CSS (SASS, Responsive Design) description',
|
||||
'link' => 'http://example.com/frontend',
|
||||
'percentage' => 95,
|
||||
'about_me_id' => $aboutMe->id,
|
||||
]);
|
||||
|
||||
Skill::create([
|
||||
'title' => 'React.js and Next.js Development',
|
||||
'image' => '/images/backend.png',
|
||||
'description' => 'React.js and Next.js Development description.',
|
||||
'link' => 'http://example.com/backend',
|
||||
'percentage' => 90,
|
||||
'about_me_id' => $aboutMe->id,
|
||||
]);
|
||||
Skill::create([
|
||||
'title' => 'Backend Development (RESTful APIs, Laravel)',
|
||||
'image' => '/images/ui.png',
|
||||
'description' => 'Backend Development (RESTful APIs, Laravel) description.',
|
||||
'link' => 'http://example.com/ui',
|
||||
'percentage' => 85,
|
||||
'about_me_id' => $aboutMe->id,
|
||||
]);
|
||||
|
||||
Skill::create([
|
||||
'title' => 'WordPress Development and Customization',
|
||||
'image' => '/images/ux.png',
|
||||
'description' => 'WordPress Development and Customization description.',
|
||||
'link' => 'http://example.com/ux',
|
||||
'percentage' => 90,
|
||||
'about_me_id' => $aboutMe->id,
|
||||
]);
|
||||
|
||||
Skill::create([
|
||||
'title' => 'BackEnd',
|
||||
'image' => '/images/backend.png',
|
||||
'description' => 'BackEnd description.',
|
||||
'link' => 'http://example.com/backend',
|
||||
'percentage' => 80,
|
||||
'about_me_id' => $aboutMe->id,
|
||||
]);
|
||||
Skill::create([
|
||||
'title' => 'UI Design',
|
||||
'image' => '/images/ui.png',
|
||||
'description' => 'UI design description.',
|
||||
'link' => 'http://example.com/ui',
|
||||
'percentage' => 70,
|
||||
'about_me_id' => $aboutMe->id,
|
||||
]);
|
||||
|
||||
Skill::create([
|
||||
'title' => 'UX Design',
|
||||
'title' => 'Version Control (Git, GitHub)',
|
||||
'image' => '/images/ux.png',
|
||||
'description' => 'UX design description.',
|
||||
'description' => 'Version Control (Git, GitHub) description.',
|
||||
'link' => 'http://example.com/ux',
|
||||
'percentage' => 83,
|
||||
'percentage' => 80,
|
||||
'about_me_id' => $aboutMe->id,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\User;
|
||||
use Database\Seeders\Blog\BlogSeeder;
|
||||
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
@@ -16,11 +17,14 @@ class DatabaseSeeder extends Seeder
|
||||
// User::factory(10)->create();
|
||||
|
||||
User::factory()->create([
|
||||
'name' => 'Test User',
|
||||
'email' => 'test@example.com',
|
||||
'name' => 'parsa aghayi',
|
||||
'email' => 'ceo@parsaaghayi.ir',
|
||||
]);
|
||||
$this->call([
|
||||
AboutMeSeeder::class,
|
||||
ProjectSeeder::class,
|
||||
ServiceSeeder::class,
|
||||
BlogSeeder::class
|
||||
// دیگر seeders
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -15,28 +15,28 @@ class ServiceSeeder extends Seeder
|
||||
{
|
||||
$services = [
|
||||
[
|
||||
'title' => 'UI/UX',
|
||||
'description' => 'Lorem ipsum dolor sit amet consectetur. Morbi diam nisi nam diam interdum',
|
||||
'title' => 'Custom Web Development',
|
||||
'description' => 'We create tailored web applications that meet your unique business requirements, utilizing the latest technologies for optimal performance.',
|
||||
'imageSrc' => '/images/ui-ux-vector.svg',
|
||||
'altText' => 'UI/UX Vector',
|
||||
'altText' => 'Custom Web Development Vector',
|
||||
],
|
||||
[
|
||||
'title' => 'Web Design',
|
||||
'description' => 'Lorem ipsum dolor sit amet consectetur. Morbi diam nisi nam diam interdum',
|
||||
'title' => 'Frontend Development',
|
||||
'description' => 'Our frontend development services focus on crafting engaging, responsive user interfaces that enhance user experience across all devices.',
|
||||
'imageSrc' => '/images/web-design-vector.svg',
|
||||
'altText' => 'Web Design Vector',
|
||||
'altText' => 'Frontend Development Vector',
|
||||
],
|
||||
[
|
||||
'title' => 'App Design',
|
||||
'description' => 'Lorem ipsum dolor sit amet consectetur. Morbi diam nisi nam diam interdum',
|
||||
'title' => 'Backend Development',
|
||||
'description' => 'We provide reliable backend development solutions, building robust server-side architectures and APIs to ensure smooth and secure application functionality.',
|
||||
'imageSrc' => '/images/app-design-vector.svg',
|
||||
'altText' => 'App Design Vector',
|
||||
'altText' => 'Backend Development Vector',
|
||||
],
|
||||
[
|
||||
'title' => 'Graphic Design',
|
||||
'description' => 'Lorem ipsum dolor sit amet consectetur. Morbi diam nisi nam diam interdum',
|
||||
'title' => 'Website Maintenance and Support',
|
||||
'description' => 'Our ongoing maintenance and support services keep your website updated, secure, and performing at its best, allowing you to focus on your core business.',
|
||||
'imageSrc' => '/images/graphic-design-vector.svg',
|
||||
'altText' => 'Graphic Design Vector',
|
||||
'altText' => 'Website Maintenance and Support Vector',
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
Reference in new issue
Block a user