diff --git a/Project/Code/app/Http/Controllers/BannerController.php b/Project/Code/app/Http/Controllers/BannerController.php index 46c6cefe..05bfea71 100644 --- a/Project/Code/app/Http/Controllers/BannerController.php +++ b/Project/Code/app/Http/Controllers/BannerController.php @@ -5,6 +5,7 @@ use App\Models\Banner; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Facades\Log; class BannerController extends Controller { @@ -16,42 +17,49 @@ public function index() } public function store(Request $request) - { - // การตรวจสอบว่าไฟล์เป็นรูปภาพและขนาดไม่เกินที่กำหนด - $request->validate([ - 'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', - ]); - - // ตรวจสอบว่าไฟล์มีอยู่ใน request หรือไม่ - if ($request->hasFile('image')) { - // อัปโหลดไฟล์ไปยัง storage แล้วเก็บเส้นทางไว้ในตัวแปร $imagePath - $imagePath = $request->file('image')->store('banners', 'public'); - } else { - return redirect()->route('banners.index')->with('error', 'กรุณาเลือกไฟล์รูปภาพ'); - } - - // สร้างแบนเนอร์ใหม่แล้วบันทึกเส้นทางรูปภาพลงในฐานข้อมูล - Banner::create([ - 'image_path' => $imagePath, - ]); - - // ส่งผู้ใช้กลับไปที่หน้าหลักและแสดงข้อความสำเร็จ - return redirect()->route('banners.index')->with('success', 'อัปโหลดรูปภาพสำเร็จ'); +{ + // ตรวจสอบว่าไฟล์ถูกส่งมาหรือไม่ + if (!$request->hasFile('image_zh')) { + return redirect()->back()->withErrors(['image_zh' => 'ไฟล์รูปภาพภาษาจีนหายไป']); } - public function destroy($id) - { - // ค้นหาข้อมูลแบนเนอร์ที่ต้องการลบ - $banner = Banner::findOrFail($id); - - // ลบไฟล์ภาพจาก storage - \Storage::delete('public/' . $banner->image_path); - - // ลบข้อมูลจากฐานข้อมูล - $banner->delete(); - - return redirect()->route('banners.index')->with('success', 'ลบรูปภาพสำเร็จ'); - } + // ตรวจสอบการอัพโหลดไฟล์ภาพทั้ง 3 ภาษา + $request->validate([ + 'image_th' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', + 'image_en' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', + 'image_zh' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', // เพิ่มการตรวจสอบสำหรับภาษาจีน + ]); + + // อัพโหลดไฟล์ภาพและบันทึก path ของไฟล์ + $imagePathTh = $request->file('image_th')->store('banners', 'public'); + $imagePathEn = $request->file('image_en')->store('banners', 'public'); + $imagePathZh = $request->file('image_zh')->store('banners', 'public'); // ภาษาจีน + + // สร้างแบนเนอร์ใหม่ในฐานข้อมูล + Banner::create([ + 'image_path_th' => $imagePathTh, + 'image_path_en' => $imagePathEn, + 'image_path_zh' => $imagePathZh, // เพิ่มการบันทึก path ของภาพภาษาจีน + ]); + + return redirect()->route('banners.index')->with('success', 'อัปโหลดรูปภาพสำเร็จ'); +} + +public function destroy($id) +{ + // ค้นหาข้อมูลแบนเนอร์ที่ต้องการลบ + $banner = Banner::findOrFail($id); + + // ลบไฟล์ภาพจาก storage + \Storage::delete('public/' . $banner->image_path_th); + \Storage::delete('public/' . $banner->image_path_en); + \Storage::delete('public/' . $banner->image_path_zh); // ลบไฟล์ภาพภาษาจีน + + // ลบข้อมูลจากฐานข้อมูล + $banner->delete(); + + return redirect()->route('banners.index')->with('success', 'ลบรูปภาพสำเร็จ'); +} } \ No newline at end of file diff --git a/Project/Code/app/Http/Controllers/ImageManagementController.php b/Project/Code/app/Http/Controllers/ImageManagementController.php index 73401f6f..021a98f6 100644 --- a/Project/Code/app/Http/Controllers/ImageManagementController.php +++ b/Project/Code/app/Http/Controllers/ImageManagementController.php @@ -1,13 +1,14 @@ back(); +{ + // ตรวจสอบว่า $lang อยู่ใน array ของภาษาที่รองรับ + if (array_key_exists($lang, Config::get('languages'))) { + Session::put('applocale', $lang); // เก็บภาษาที่เลือกใน session + App::setLocale($lang); // เปลี่ยนภาษาใน app } + return redirect()->back(); // กลับไปหน้าก่อนหน้า +} } \ No newline at end of file diff --git a/Project/Code/app/Models/Banner.php b/Project/Code/app/Models/Banner.php index 6a0b822a..9a9b00b3 100644 --- a/Project/Code/app/Models/Banner.php +++ b/Project/Code/app/Models/Banner.php @@ -7,7 +7,5 @@ class Banner extends Model { use HasFactory; - - // กำหนดให้สามารถกรอกข้อมูลได้จากฟอร์ม - protected $fillable = ['image_path']; + protected $fillable = ['image_path_th','image_path_en','image_path_zh']; } \ No newline at end of file diff --git a/Project/Code/app/Providers/AppServiceProvider.php b/Project/Code/app/Providers/AppServiceProvider.php index be8ae3a7..d7276f1a 100644 --- a/Project/Code/app/Providers/AppServiceProvider.php +++ b/Project/Code/app/Providers/AppServiceProvider.php @@ -4,6 +4,8 @@ use Illuminate\Support\ServiceProvider; use Illuminate\Pagination\Paginator; +use Illuminate\Support\Facades\Config; +use Illuminate\Support\Facades\App; class AppServiceProvider extends ServiceProvider { @@ -23,13 +25,23 @@ public function register() * @return void */ public function boot() - { - Paginator::useBootstrap(); - view()->composer( - 'layouts.layout', - function ($view) { - $view->with('dn', \App\Models\Program::where('degree_id', '=', 1)->get()); - } - ); +{ + Paginator::useBootstrap(); + + // ตั้งค่าภาษาโดยใช้ session หรือค่าเริ่มต้น 'th' + $locale = session('applocale', 'th'); // ถ้า session ไม่มี ค่าปริยายจะเป็น 'th' + + // ตรวจสอบว่า locale ที่ตั้งค่านั้นอยู่ในรายการภาษาที่รองรับหรือไม่ + if (array_key_exists($locale, Config::get('languages'))) { + App::setLocale($locale); } + + // ส่งข้อมูลให้กับทุกๆ View ที่ใช้ layout 'layouts.layout' + view()->composer( + 'layouts.layout', + function ($view) { + $view->with('dn', \App\Models\Program::where('degree_id', '=', 1)->get()); + } + ); +} } diff --git a/Project/Code/config/languages.php b/Project/Code/config/languages.php index f8db35a2..b84d8959 100644 --- a/Project/Code/config/languages.php +++ b/Project/Code/config/languages.php @@ -10,5 +10,9 @@ 'display' => 'ไทย', 'flag-icon' => 'th' ], + 'zh' => [ + 'display' => '中文', + 'flag-icon' => 'cn' // เพิ่มภาษาจีนและกำหนดธงชาติ + ], ]; diff --git a/Project/Code/database/migrations/2025_02_24_003843_create_banners_table.php b/Project/Code/database/migrations/2025_02_24_003843_create_banners_table.php index 87384ffb..92eabee1 100644 --- a/Project/Code/database/migrations/2025_02_24_003843_create_banners_table.php +++ b/Project/Code/database/migrations/2025_02_24_003843_create_banners_table.php @@ -15,18 +15,15 @@ public function up() { Schema::create('banners', function (Blueprint $table) { $table->id(); - $table->string('image_path'); // เก็บ path ของรูป + $table->string('image_path_th')->nullable(); + $table->string('image_path_en')->nullable(); + $table->string('image_path_zh')->nullable(); $table->timestamps(); }); } - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::dropIfExists('banners'); - } +public function down() +{ + Schema::dropIfExists('banners'); +} } diff --git a/Project/Code/resources/views/home.blade.php b/Project/Code/resources/views/home.blade.php index 935ddf4d..1d6c5f54 100644 --- a/Project/Code/resources/views/home.blade.php +++ b/Project/Code/resources/views/home.blade.php @@ -44,7 +44,22 @@