Laravel - อัปเดตบันทึก

เราสามารถอัปเดตบันทึกโดยใช้ไฟล์ DB ซุ้มด้วย updateวิธี. ไวยากรณ์ของวิธีการอัพเดตมีดังแสดงในตารางต่อไปนี้

ไวยากรณ์ ปรับปรุง int (สตริง $ แบบสอบถามอาร์เรย์ $ bindings = array ())
พารามิเตอร์
  • $ query (string) - เคียวรีเพื่อดำเนินการในฐานข้อมูล
  • $ bindings (array) - ค่าที่จะผูกกับแบบสอบถาม
ผลตอบแทน int
คำอธิบาย รันคำสั่งอัพเดตกับฐานข้อมูล

ตัวอย่าง

ดูตัวอย่างต่อไปนี้เพื่อทำความเข้าใจเพิ่มเติมเกี่ยวกับการอัปเดตระเบียน -

Step 1 - ดำเนินการคำสั่งด้านล่างเพื่อสร้างคอนโทรลเลอร์ที่เรียกว่า StudViewController.

php artisan make:controller StudUpdateController --plain

Step 2 - หลังจากดำเนินการสำเร็จคุณจะได้รับผลลัพธ์ต่อไปนี้ -

Step 3 - คัดลอกรหัสต่อไปนี้ไปยังไฟล์ app/Http/Controllers/ StudUpdateController.php

app/Http/Controllers/StudUpdateController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class StudUpdateController extends Controller {
   public function index() {
      $users = DB::select('select * from student');
      return view('stud_edit_view',['users'=>$users]);
   }
   public function show($id) {
      $users = DB::select('select * from student where id = ?',[$id]);
      return view('stud_update',['users'=>$users]);
   }
   public function edit(Request $request,$id) {
      $name = $request->input('stud_name');
      DB::update('update student set name = ? where id = ?',[$name,$id]);
      echo "Record updated successfully.<br/>";
      echo '<a href="/https/isolution.pro/edit-records">Click Here</a> to go back.';
   }
}

Step 4 - สร้างไฟล์ดูชื่อ

resources/views/stud_edit_view.blade.php และคัดลอกรหัสต่อไปนี้ในไฟล์นั้น

resources/views/stud_edit_view.blade.php

<html>
   <head>
      <title>View Student Records</title>
   </head>
   
   <body>
      
      <table border = "1">
         <tr>
            <td>ID</td>
            <td>Name</td>
            <td>Edit</td>
         </tr>
         @foreach ($users as $user)
         <tr>
            <td>{{ $user->id }}</td>
            <td>{{ $user->name }}</td>
            <td><a href = 'edit/{{ $user->id }}'>Edit</a></td>
         </tr>
         @endforeach
      </table>
   </body>
</html>

Step 5 - สร้างไฟล์มุมมองอื่นที่เรียกว่า

resources/views/stud_update.php และคัดลอกรหัสต่อไปนี้ในไฟล์นั้น

resources/views/stud_update.php

<html>
   
   <head>
      <title>Student Management | Edit</title>
   </head>
   
   <body>
      <form action = "/edit/<?php echo $users[0]->id; ?>" method = "post">
         <input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
      
         <table>
            <tr>
               <td>Name</td>
               <td>
                  <input type = 'text' name = 'stud_name' 
                     value = '<?php echo$users[0]->name; ?>'/>
               </td>
            </tr>
            <tr>
               <td colspan = '2'>
                  <input type = 'submit' value = "Update student" />
               </td>
            </tr>
         </table>
      </form>
   </body>
</html>

Step 6 - Add บรรทัดต่อไปนี้ใน app/Http/routes.php.

app/Http/routes.php.

Route::get('edit-records','StudUpdateController@index');
Route::get('edit/{id}','StudUpdateController@show');
Route::post('edit/{id}','StudUpdateController@edit');

Step 7 - ไปที่ URL ต่อไปนี้เพื่ออัปเดตบันทึกในฐานข้อมูล

http://localhost:8000/edit-records

Step 8 - ผลลัพธ์จะปรากฏดังที่แสดงในภาพต่อไปนี้

Step 9 - คลิกลิงก์แก้ไขในบันทึกใด ๆ และคุณจะถูกเปลี่ยนเส้นทางไปยังหน้าที่คุณสามารถแก้ไขบันทึกนั้นได้

Step 10 - ผลลัพธ์จะปรากฏดังที่แสดงในภาพต่อไปนี้

Step 11 - หลังจากแก้ไขบันทึกนั้นคุณจะเห็นข้อความแจ้งดังที่แสดงในภาพต่อไปนี้


Laravel Tutorial

Laravel แหล่งข้อมูลที่เป็นประโยชน์

Language