Merge branch 'develop'
This commit is contained in:
commit
ac4bcbb5bf
119
app/Http/Controllers/Website/ProjectController.php
Normal file
119
app/Http/Controllers/Website/ProjectController.php
Normal file
@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\website;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Website\Project;
|
||||
use App\Models\Website\ProjectCategory;
|
||||
|
||||
class ProjectController extends Controller
|
||||
{
|
||||
public function index($locale): \Illuminate\Http\JsonResponse
|
||||
{
|
||||
// Read per_page from query string or set a default value
|
||||
$perPage = request()->query('per_page', 10); // مقدار پیشفرض ۱۰ در نظر گرفته میشود
|
||||
|
||||
// Retrieve projects with translations and categories
|
||||
$projects = Project::with('translations', 'category.translations')->paginate($perPage);
|
||||
$formattedProjects = $projects->items();
|
||||
|
||||
$formattedProjects = array_map(function ($project) use ($locale) {
|
||||
$translation = $project->translations->firstWhere('locale', $locale);
|
||||
$categoryTranslation = $project->category->translations->firstWhere('locale', $locale);
|
||||
|
||||
return [
|
||||
'id' => $project->id,
|
||||
'title' => $translation ? $translation->title : null,
|
||||
'category' => $categoryTranslation ? $categoryTranslation->title : null,
|
||||
'images' => [
|
||||
['src' => $project->image1, 'position' => '1', 'zIndex' => 1],
|
||||
['src' => $project->image2, 'position' => '2', 'zIndex' => 2],
|
||||
],
|
||||
'created_at' => $project->created_at,
|
||||
'updated_at' => $project->updated_at,
|
||||
];
|
||||
}, $formattedProjects);
|
||||
|
||||
return response()->json([
|
||||
'projects' => [
|
||||
'current_page' => $projects->currentPage(),
|
||||
'data' => $formattedProjects,
|
||||
'total' => $projects->total(),
|
||||
'last_page' => $projects->lastPage(),
|
||||
],
|
||||
'categories' => ProjectCategory::all()->map(function ($category) use ($locale) {
|
||||
return [
|
||||
'id' => $category->id,
|
||||
'title' => $category->translations->firstWhere('locale', $locale)->title ?? null,
|
||||
'image' => $category->image,
|
||||
];
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$project = Project::create($request->only(['category_id', 'image1', 'image2']));
|
||||
|
||||
// ایجاد ترجمهها
|
||||
foreach (['fa', 'en'] as $locale) {
|
||||
$project->translations()->create([
|
||||
'locale' => $locale,
|
||||
'title' => $request->input("title_$locale"),
|
||||
'description' => $request->input("description_$locale"),
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json($project->load('translations'), 201);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$project = Project::with('translations', 'category.translations')->findOrFail($id);
|
||||
|
||||
$translation = $project->translations->firstWhere('locale', 'fa');
|
||||
$categoryTranslation = $project->category->translations->firstWhere('locale', 'fa');
|
||||
|
||||
$formattedProject = [
|
||||
'id' => $project->id,
|
||||
'title' => $translation ? $translation->title : null,
|
||||
'category' => $categoryTranslation ? $categoryTranslation->title : null,
|
||||
'images' => [
|
||||
['src' => $project->image1, 'position' => '1', 'zIndex' => 1],
|
||||
['src' => $project->image2, 'position' => '2', 'zIndex' => 2],
|
||||
],
|
||||
'created_at' => $project->created_at,
|
||||
'updated_at' => $project->updated_at,
|
||||
];
|
||||
|
||||
return response()->json(['project' => $formattedProject]);
|
||||
}
|
||||
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$project = Project::findOrFail($id);
|
||||
$project->update($request->only(['category_id', 'image1', 'image2']));
|
||||
|
||||
// بهروزرسانی ترجمهها
|
||||
foreach (['fa', 'en'] as $locale) {
|
||||
$project->translations()->updateOrCreate(
|
||||
['locale' => $locale],
|
||||
[
|
||||
'title' => $request->input("title_$locale"),
|
||||
'description' => $request->input("description_$locale"),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
return response()->json($project->load('translations'));
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
Project::destroy($id);
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
}
|
||||
24
app/Models/Website/Project.php
Normal file
24
app/Models/Website/Project.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Website;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Project extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'website_projects'; // نام صحیح جدول
|
||||
protected $fillable = ['category_id', 'image1', 'image2'];
|
||||
|
||||
public function translations()
|
||||
{
|
||||
return $this->hasMany(ProjectTranslation::class, 'project_id');
|
||||
}
|
||||
|
||||
public function category()
|
||||
{
|
||||
return $this->belongsTo(ProjectCategory::class, 'category_id');
|
||||
}
|
||||
}
|
||||
19
app/Models/Website/ProjectCategory.php
Normal file
19
app/Models/Website/ProjectCategory.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Website;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ProjectCategory extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'website_project_categories'; // نام صحیح جدول
|
||||
protected $fillable = ['image'];
|
||||
|
||||
public function translations()
|
||||
{
|
||||
return $this->hasMany(ProjectCategoryTranslation::class, 'category_id');
|
||||
}
|
||||
}
|
||||
14
app/Models/Website/ProjectCategoryTranslation.php
Normal file
14
app/Models/Website/ProjectCategoryTranslation.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Website;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ProjectCategoryTranslation extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'website_project_category_translations'; // نام صحیح جدول
|
||||
protected $fillable = ['category_id', 'locale', 'title', 'description'];
|
||||
}
|
||||
19
app/Models/Website/ProjectTranslation.php
Normal file
19
app/Models/Website/ProjectTranslation.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Website;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ProjectTranslation extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'website_project_translations'; // نام صحیح جدول
|
||||
protected $fillable = ['project_id', 'locale', 'title', 'description'];
|
||||
|
||||
public function project()
|
||||
{
|
||||
return $this->belongsTo(Project::class, 'project_id');
|
||||
}
|
||||
}
|
||||
@ -15,8 +15,8 @@ public function up(): void
|
||||
Schema::create('website_projects', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('category_id')->constrained('website_project_categories')->onDelete('cascade'); // کلید خارجی به جدول categories
|
||||
$table->string('image1'); // عنوان ترجمهشده
|
||||
$table->string('image2'); // عنوان ترجمهشده
|
||||
$table->string('image1');
|
||||
$table->string('image2');
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
|
||||
@ -5,6 +5,8 @@
|
||||
use App\Models\User;
|
||||
use Database\Seeders\Blog\BlogSeeder;
|
||||
use Database\Seeders\Website\AboutMeSeeder;
|
||||
use Database\Seeders\Website\ProjectCategorySeeder;
|
||||
use Database\Seeders\Website\ProjectSeeder;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class DatabaseSeeder extends Seeder
|
||||
@ -22,6 +24,8 @@ public function run(): void
|
||||
]);
|
||||
$this->call([
|
||||
AboutMeSeeder::class,
|
||||
// ProjectSeeder::class,
|
||||
ProjectCategorySeeder::class,
|
||||
ProjectSeeder::class,
|
||||
ServiceSeeder::class,
|
||||
BlogSeeder::class
|
||||
|
||||
@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
use App\Models\Project;
|
||||
|
||||
class ProjectSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
Project::factory()->count(100)->create();
|
||||
}
|
||||
}
|
||||
41
database/seeders/Website/ProjectCategorySeeder.php
Normal file
41
database/seeders/Website/ProjectCategorySeeder.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders\Website;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use App\Models\Website\ProjectCategory;
|
||||
use App\Models\Website\ProjectCategoryTranslation;
|
||||
|
||||
class ProjectCategorySeeder extends Seeder
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$categories = [
|
||||
['fa' => 'طراحی اپلیکیشن', 'en' => 'App Design'],
|
||||
['fa' => 'فرانت اند', 'en' => 'Front End'],
|
||||
['fa' => 'بک اند', 'en' => 'Backend'],
|
||||
['fa' => 'طراحی وب', 'en' => 'Web Design'],
|
||||
['fa' => 'طراحی گرافیک', 'en' => 'Graphic Design'],
|
||||
];
|
||||
|
||||
foreach ($categories as $key => $category) {
|
||||
// ایجاد دستهبندی اصلی
|
||||
$createdCategory = ProjectCategory::create(['image' => 'category' . ($key + 1) . '.jpg']);
|
||||
|
||||
// ایجاد ترجمهها
|
||||
ProjectCategoryTranslation::create([
|
||||
'category_id' => $createdCategory->id,
|
||||
'locale' => 'fa',
|
||||
'title' => $category['fa'],
|
||||
'description' => 'توضیحات ' . $category['fa'],
|
||||
]);
|
||||
|
||||
ProjectCategoryTranslation::create([
|
||||
'category_id' => $createdCategory->id,
|
||||
'locale' => 'en',
|
||||
'title' => $category['en'],
|
||||
'description' => 'Description for ' . $category['en'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
46
database/seeders/Website/ProjectSeeder.php
Normal file
46
database/seeders/Website/ProjectSeeder.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders\Website;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use App\Models\Website\Project;
|
||||
use App\Models\Website\ProjectTranslation;
|
||||
use App\Models\Website\ProjectCategory;
|
||||
|
||||
class ProjectSeeder extends Seeder
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
// دریافت تمام دستهبندیها
|
||||
$categories = ProjectCategory::all();
|
||||
|
||||
foreach ($categories as $category) {
|
||||
// ایجاد تعداد تصادفی پروژه برای هر دستهبندی
|
||||
$projectCount = rand(5, 10);
|
||||
|
||||
for ($i = 1; $i <= $projectCount; $i++) {
|
||||
// ایجاد پروژه جدید
|
||||
$project = Project::create([
|
||||
'category_id' => $category->id,
|
||||
'image1' => 'project' . $i . '_image1.jpg',
|
||||
'image2' => 'project' . $i . '_image2.jpg'
|
||||
]);
|
||||
|
||||
// ایجاد ترجمهها
|
||||
ProjectTranslation::create([
|
||||
'project_id' => $project->id,
|
||||
'locale' => 'fa',
|
||||
'title' => 'پروژه ' . $i . ' - ' . $category->translations()->where('locale', 'fa')->first()->title,
|
||||
'description' => 'توضیحات پروژه ' . $i . ' برای دستهبندی ' . $category->translations()->where('locale', 'fa')->first()->title,
|
||||
]);
|
||||
|
||||
ProjectTranslation::create([
|
||||
'project_id' => $project->id,
|
||||
'locale' => 'en',
|
||||
'title' => 'Project ' . $i . ' - ' . $category->translations()->where('locale', 'en')->first()->title,
|
||||
'description' => 'Description for project ' . $i . ' in category ' . $category->translations()->where('locale', 'en')->first()->title,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -4,7 +4,7 @@
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
|
||||
<title>Laravel</title>
|
||||
<title>Laravel v1</title>
|
||||
|
||||
<!-- Fonts -->
|
||||
<link rel="preconnect" href="https://fonts.bunny.net">
|
||||
|
||||
@ -2,27 +2,33 @@
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use App\Http\Controllers\ProjectController;
|
||||
use App\Http\Controllers\Blog\CategoryController;
|
||||
use App\Http\Controllers\Blog\HomeController;
|
||||
use App\Http\Controllers\Blog\PostController;
|
||||
use App\Http\Controllers\Blog\TagController;
|
||||
use App\Http\Controllers\SkillController;
|
||||
// use App\Http\Controllers\SkillController;
|
||||
use App\Http\Controllers\Website\ServiceController;
|
||||
use App\Http\Controllers\Website\AboutMeController;
|
||||
use App\Http\Controllers\Website\ProjectController;
|
||||
|
||||
Route::get('/user', function (Request $request) {
|
||||
return $request->user();
|
||||
})->middleware('auth:sanctum');
|
||||
|
||||
Route::resource('projects', ProjectController::class);
|
||||
// Route::resource('projects', ProjectController::class);
|
||||
|
||||
Route::resource('skills', SkillController::class);
|
||||
Route::apiResource('services', ServiceController::class);
|
||||
Route::prefix('website')->group(function () {
|
||||
Route::get('about-me/{id}/translation/{locale}', [AboutMeController::class, 'getTranslation']);
|
||||
Route::get('services/{locale}', [ServiceController::class, 'index']); // مسیر جدید برای لیست خدمات با ترجمه
|
||||
Route::get('services/{id}/translation/{locale}', [ServiceController::class, 'showTranslation']);
|
||||
Route::group(['prefix' => 'projects'], function () {
|
||||
Route::get('/translation/{locale}', [ProjectController::class, 'index']);
|
||||
Route::get('/{id}/translation/{locale}', [ProjectController::class, 'show']);
|
||||
Route::post('/', [ProjectController::class, 'store']);
|
||||
Route::put('/{id}', [ProjectController::class, 'update']);
|
||||
Route::delete('/{id}', [ProjectController::class, 'destroy']);
|
||||
});
|
||||
});
|
||||
|
||||
// مسیرهای بلاگ
|
||||
|
||||
Loading…
Reference in New Issue
Block a user