วิธีการ Insert โดยผ่านตัว Model Laravel Framework
Step 1 - ให้เข้าไปที่ route/web.php , ให้ไปตั้งค่า Routing สำหรับ Insert ตัวอย่าง Code :
โค้ด: เลือกทั้งหมด
Route::post('/createAirline','Controller_show_airline@store');
โค้ด: เลือกทั้งหมด
<div class="table-responsive">
<h2>เพิ่มข้อมูลตารางสายการบิน</h2>
<form action="/createAirline" method="post" enctype="multipart/form-data"> <!-- enctype="multipart.... ไว้สำหรับการ Upload ข้อมูล -->
{{csrf_field()}}
<div class="form-group">
<label for="name_airline">ชื่อสายการบิน</label>
<input type="text" class="form-control" name="name_airline" id="name_airline" placeholder="Name Airline">
</div>
<div class="form-group">
<label for="description">ชื่อบริษัทสายการบิน</label>
<input type="text" class="form-control" name="name_comp" id="name_comp" placeholder="Name Company">
</div>
<div class="form-group">
<label for="image">รูปภาพ</label>
<input type="file" class="form-control" name="image_name" id="image">
</div>
<button type="submit" name="submit" class="btn btn-success">Submit</button>
</form>
</div>
โค้ด: เลือกทั้งหมด
<div class="d-flex flex-column flex-md-row align-items-center p-3 px-md-4 mb-3 bg-white border-bottom shadow-sm">
<a href="/createAirline" class="btn btn-primary">เพิ่มข้อมูล</a>
</div>
ตัวอย่าง Code :
โค้ด: เลือกทั้งหมด
php artisan make:controller Controller_show_airline.php --resource
Step 6 - จากนั้นไปที่ Function store ที่ใช้สำหรับในการเพิ่มข้อมูล โดยเราจะเพิ่มคำสั่งดังนี้ในการเพิ่มข้อมูลลงไป ตัวอย่าง Code Function store() :
โค้ด: เลือกทั้งหมด
public function store(Request $request)
{
$request->validate([
'name_airline' => 'required',
'name_comp' => 'required',
'image_name' => 'required|file|image|mimes:jpeg,png,jpg|max:5000',
//dd($request->name);
]);
//--------------------- Convert to image -------------------------//
$stringImageReformat=base64_encode('_'.time()); //หากมีการ upload ชื่อ file ซ้ำ ให้ทำการเข้ารหัส base64_encode() ...
//.... เช่น a.jpg = axafaf.jpg พอ upload ซ้ำ ก็จะได้ a.jpge = asfff.jpg ....
//.... มีการกำหนด string '__' แล้วใช้ Function time() ใช้ตรวจสอบ วันที่ Upload file
//dd($stringImageReformat);
$ext = $request->file('image_name')->getClientOriginalExtension(); //ดึงนามสกุล file เข้ามาทำงาน โดยใช้ Function getClientOriginalExtension() ....
//... จะมีการ Return ค่า กลับไปยัง ชื่อ File เดิม ที่ uploade แล้ว ซึ่ง ....
//... ชื่อเดิมก็คือ ค่า empty , ก็จะเหลือแค่ นามสกุล File ที่เอาไว้ใช้งาน
//dd($ext);
$imagename = $stringImageReformat.".".$ext; //ทำการรวมตัวแปร $ext กับ $stringImageReformat
//dd($imagename); //สุ่มเข้ารหัสไปเรื่อยๆ เช่น "XzE1OTA3MDQyMzc=.jpg"
//--------------------- Upload File ใน Local(Your computer) เพื่อ นำ File มาแสดงข้อมูล -------------------------//
//ต้นทาง
$imageEncoded = File::get($request->image_name); //เรียกใช้งาน Module File โดยเอา File ภาพ ไปเก็บไว้ในตัวแปร , ส่วน ....
//... $request รับค่ามาจาก Form view , ส่วน image_name คือ ชื่อ Form view
//ปลายทาง
Storage::disk('local')->put('public/airline_image/'.$imagename,$imageEncoded); //Upload File ไปเก็บไว้ใน Folder ปลายทาง โดยระบุปลายทาง public/airline_image/
//ref - https://laravel.com/docs/6.x/filesystem#storing-files
//--------------------- Insert -------------------------//
$airline = New Airline;
$airline->name_airline = $request->name_airline;
$airline->name_comp = $request->name_comp;
$airline->image = $imagename; //ไม่ได้จะเอา File จากการ Upload file ไปเก็บไว้ใน Tables ....
//... แต่จะเอาแค่ $imagename ไปเก็บไว้ใน Tables เฉยๆ
$airline->save();
//--------------------- flash message -------------------------//
Session()->flash("success","บันทึกข้อมูลเรียบร้อยแล้ว !"); //สร้าง สถานะ action ไว้สำหรับแจ้งเตือนข้อความ เช่น เพิ่มข้อมูล,แก้ไข,ลบ ...
//... ส่งการแจ้งเตือนไปยัง resources/view/layouts/master.blade.php
//- ref - https://laravel.com/docs/7.x/session#flash-data
return redirect('/showAirline');
}
Step 8 - แล้วทำการกดปุ่มเพิ่มข้อมูลลงไป ผลลัพธ์ที่ได้
************* มี Post ต่อจาก Post เดิม **************