Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 42 additions & 34 deletions Project/Code/app/Http/Controllers/BannerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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', 'ลบรูปภาพสำเร็จ');
}


}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

namespace App\Http\Controllers;

use App\Models\Banner;
use Illuminate\Http\Request;

class ImageManagementController extends Controller
{
public function index()
{
return view('image_management.index'); // ส่งไปที่ Blade Template
$banners = Banner::all();
return view('image_management.index', compact('banners')); // ส่งไปที่ Blade Template
}
}
12 changes: 7 additions & 5 deletions Project/Code/app/Http/Controllers/LocalizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ public function index()
// return view('welcome');
}
public function switchLang($lang)
{
if (array_key_exists($lang, Config::get('languages'))) {
Session::put('applocale', $lang);
}
return redirect()->back();
{
// ตรวจสอบว่า $lang อยู่ใน array ของภาษาที่รองรับ
if (array_key_exists($lang, Config::get('languages'))) {
Session::put('applocale', $lang); // เก็บภาษาที่เลือกใน session
App::setLocale($lang); // เปลี่ยนภาษาใน app
}
return redirect()->back(); // กลับไปหน้าก่อนหน้า
}
}
4 changes: 1 addition & 3 deletions Project/Code/app/Models/Banner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
28 changes: 20 additions & 8 deletions Project/Code/app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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());
}
);
}
}
4 changes: 4 additions & 0 deletions Project/Code/config/languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
'display' => 'ไทย',
'flag-icon' => 'th'
],
'zh' => [
'display' => '中文',
'flag-icon' => 'cn' // เพิ่มภาษาจีนและกำหนดธงชาติ
],

];
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Loading