Laravel 7 : การใช้ Model แก้ไขและลบข้อมูล (Update, Delete)

ในตอนนี้ลองมาหัดใช้ Laravel Model เพื่อแก้ไข (update) และลบ (delete) ข้อมูลในฐานข้อมูลกัน

ใน Laravel จะมีหลายวิธีที่เพื่อแก้ไขหรือลบข้อมูล ในที่นี้ขอใช้วิธีการค้นหาออปเจ็ค Article ที่ต้องการด้วยเมธอด find() แล้วใช้เมธอด update() หรือ delete() เพื่อแก้ไข หรือลบข้อมูล

update() แก้ไขข้อมูล

ตอนนี้เรามีข้อมูล articles อยู่ 3 แถว

mysql> SELECT * FROM articles;
+----+--------------------+-----------------------------+---------------------+---------------------+
| id | title              | body                        | created_at          | updated_at          |
+----+--------------------+-----------------------------+---------------------+---------------------+
|  1 | The First article  | Body of the first article.  | 2020-04-11 13:01:53 | 2020-04-11 13:01:53 |
|  2 | The Second article | Body of the second article. | 2020-04-11 13:07:29 | 2020-04-11 13:07:29 |
|  3 | The Third article  | Body of the third article.  | 2020-04-11 13:07:35 | 2020-04-11 13:07:35 |
+----+--------------------+-----------------------------+---------------------+---------------------+
3 rows in set (0.00 sec)

ตัวอย่างการใช้ tinker เพื่อแก้ไขค่าของฟิลด์ title และ body ของข้อมูลที่มีค่า id เท่ากับ 3

ขั้นแรก ใช้เมธอด find(3) แล้วเก็บผลลัพธ์ไว้ในตัวแปรออปเจ็ค $article

$ php artisan tinker
Psy Shell v0.9.12 (PHP 7.2.28 — cli) by Justin Hileman
>>> $article = App\Article::find(3);
=> App\Article {#3019
     id: 3,
     title: "The Third article",
     body: "Body of the third article.",
     created_at: "2020-04-11 13:07:35",
     updated_at: "2020-04-11 13:07:35",
   }
>>>

แล้วก็ใช้เมธอด update() ของตัวแปรออปเจ็คนี้ ระบุค่าฟิลด์ที่ต้องการแก้ไข ในรูปแบบ Associate Array

>>> $article->update([
...     'title' => 'Updated third article',
...     'body' => 'The body also updated',
... ]);
=> true

หากลอง query ข้อมูลด้วยคำสั่ง mysql ก็จะเห็นข้อมูลที่ถูกแก้ไข

SELECT * FROM articles;
+----+-----------------------+-----------------------------+---------------------+---------------------+
| id | title                 | body                        | created_at          | updated_at          |
+----+-----------------------+-----------------------------+---------------------+---------------------+
|  1 | The First article     | Body of the first article.  | 2020-04-11 13:01:53 | 2020-04-11 13:01:53 |
|  2 | The Second article    | Body of the second article. | 2020-04-11 13:07:29 | 2020-04-11 13:07:29 |
|  3 | Updated third article | The body also updated       | 2020-04-11 13:07:35 | 2020-04-12 15:44:47 |
+----+-----------------------+-----------------------------+---------------------+---------------------+
3 rows in set (0.00 sec)

สังเกตว่า นอกจากข้อมูลในฟิลด์ title และ body จะถูกปรับปรุงแล้ว ฟิลด์ updated_at ก็จะถูกปรับปรุงเป็นเวลา ณ ตอนที่รันคำสั่ง update() ด้วย

delete() ลบข้อมูล

การลบข้อมูล ก็ทำในลักษณะเดียวกันกับการปรับปรุง คือใช้เมธอด find() ค้นหาข้อมูลที่เราต้องการลบ แล้วก็เรียกใช้เมธอด delete() เพื่อลบข้อมูล

ตัวอย่างเช่น เราต้องการลบข้อมูลที่มีค่า id เท่ากับ 2

ขั้นแรก ใช้เมธอด find(2) แล้วเก็บผลลัพธ์ไว้ในตัวแปรออปเจ็ค $article

$ php artisan tinker
Psy Shell v0.9.12 (PHP 7.2.28 — cli) by Justin Hileman
>>> $article = App\Article::find(2);
=> App\Article {#3019
     id: 2,
     title: "The Second article",
     body: "Body of the second article.",
     created_at: "2020-04-11 13:07:29",
     updated_at: "2020-04-11 13:07:29",
   }

แล้วก็ใช้เมธอด delete() ของตัวแปรออปเจ็คนี้ เพื่อลบข้อมูล article นี้ ออกจากฐานข้อมูล

>>> $article->delete();
=> true

ทดลอง query ข้อมูลด้วยคำสั่ง mysql ก็จะไม่มีข้อมูล id เท่ากับ 2 แล้ว

mysql> SELECT * FROM articles;
+----+-----------------------+----------------------------+---------------------+---------------------+
| id | title                 | body                       | created_at          | updated_at          |
+----+-----------------------+----------------------------+---------------------+---------------------+
|  1 | The First article     | Body of the first article. | 2020-04-11 13:01:53 | 2020-04-11 13:01:53 |
|  3 | Updated third article | The body also updated      | 2020-04-11 13:07:35 | 2020-04-12 15:44:47 |
+----+-----------------------+----------------------------+---------------------+---------------------+
2 rows in set (0.00 sec)

ถึงตอนนี้เราก็สามารถเรียกใช้งาน Laravel Model เพื่อเพิ่ม อ่าน แก้ไข และ ลบข้อมูลเบื้องต้นได้แล้ว

ในตอนต่อไป เรามาดูวิธีการสร้าง HTML Form เพื่อกรอกข้อมูลผ่านหน้าเว็บกัน

ข้อมูลเพิ่มเติม

Laravel Update Model