laravel 5.4 การ Creating, reading, updating, and deleting (CRUD) โดยใช้ Controllers

26 ก.ค. 2017 , 9,861 Views   , หมวดหมู่ Laravel ทุกหมวดหมู่ โค๊ดดิ้ง   , ป้ายกำกับ:


เตรียมฐานข้อมูลให้พร้อม

เริ่มจากเตรียม Database ก่อนโดยเราจะใช้ตารางที่ชื่อ blogs โดยดูการสร้างจากหัวข้อนี้ laravel 5.4 การใช้งาน MIGRATIONS และ SEEDING

สร้าง Eloquent Model สำหรับ Blogs (ดูเพิ่มเติม)

โดยเข้าไปในโฟลเดอร์ app\Models หากไม่มีโฟลเดอร์ Models ก็ให้สร้างขึ้นมา แล้วสร้างไฟล์ชื่อ Blog.php ใว้ในโฟลเดอร์ Models

* การตั้งชื่อไฟล์จะใช้ชื่อตารางโดยตัด(s)ออก เช่น ตารางชื่อ blogs ก็จะตั้งชื่อเป็น Blog.php แต่หากจะตั้งแบบอื่นก็ได้ แล้วระบุชื่อตารางที่ต้องการ เช่น

มาเขียนต่อครับ ในไฟล์ Blog.php ให้เพิ่มโค๊ดดังนี้

เราก็จะได้ Eloquent Model ที่ลิ้งค์กับตาราง blogs โดยสามารถเข้าถึงจาก Controllers ได้

สร้าง Controllers

โดยใช้ resource controller (ดูเพิ่มเติม) ใช้คำสั่ง Command Prompt ด้านล่างเพื่อสร้าง

หรือเข้าไปในโฟลเดอร์ app\Http\Controllers แล้วสร้างไฟล์ชื่อ BlogController.php ในไฟล์เขียนโค๊ดดังนี้

เนื่องจากเราสร้าง Model Blog ใว้ในโฟลเดอร์ app/Models ดังนั้นเราจึงต้องบอกให้ Controller รู้ว่าเราจะใช้ app/Models/Blog โดยกำหนด use App\Models\Blog; ใว้ที่ด้านบนของ Controller ดังนี้

กำหนดค่า Routes

เปิดไฟล์ routes/web.php แล้วเพิ่มโค๊ดดังนี้

โดยระบบจะกำหนด URL และ Action ในการ Creating, reading, updating, and deleting ให้อัตโนมัติ ตามตารางด้านล่าง

Actions Handled By the Controller

HTTP Verb Path (URL) Action (Method) Route Name
GET /blogs index blogs.index
GET /blogs/create create blogs.create
POST /blogs store blogs.store
GET /blogs/{id} show blogs.show
GET /blogs/{id}/edit edit blogs.edit
PUT/PATCH /blogs/{id} update blogs.update
DELETE /blogs/{id} destroy blogs.destroy

*สามารถกำหนดใช้เฉพาะบาง Action ใน Routes ได้ เช่น

View

ให้สร้าง view เพื่อใช้แสดงผลข้อมูลดังนี้

resources

  • views
    • blogs
      • index.blade.php
      • create.blade.php
      • show.blade.php
      • edit.blade.php

หน้าแสดงทุกเรคคอร์ด

Description URL Controller Function View File
Default page for showing all the blogs. GET example.com/blogs index() resources/views/blogs/index.blade.php

Controller Function index() app\Http\Controllers\BlogController.php

View resources/views/blogs/index.blade.php

หน้าสร้างเรคคอร์ด

Description URL Controller Function View File
Show the form to create a blogs. GET example.com/blogs/create create() resources/views/blogs/create.blade.php

Controller Function create() app\Http\Controllers\BlogController.php

View resources/views/blogs/create.blade.php

ฟังค์ชันด้านบนสำหรับแสดงฟอร์มเพื่อกรอกข้อมูล เมื่อทำการคลิก Submit Form ระบบจะไปทำงานต่อที่ method store()

ตรวจสอบและสร้างข้อมูล

Description URL Controller Function View File
Process the create form submit and save the blog to the database. POST example.com/blog store() NONE

Controller Function store() app\Http\Controllers\BlogController.php

หน้าแสดงข้อมูลเรคคอร์ด

Description URL Controller Function View File
Show one of the nerds. GET example.com/nerds/{id} show() app/views/nerds/show.blade.php

Controller Function show() app\Http\Controllers\BlogController.php

View resources/views/blogs/show.blade.php

หน้าฟอร์มแก้ไขข้อมูล

Description URL Controller Function View File
Pull blog from the database and allow editing. GET example.com/blogs/{id}/edit edit() resources/views/blogs/edit.blade.php

Controller Function edit() app\Http\Controllers\BlogController.php

View resources/views/blogs/edit.blade.php

 

หลังจากกดปุ่มอัพเดทแล้วระบบจะไปทำงานต่อที่ method update()

ตรวจสอบและอัพเดทข้อมูล

Description URL Controller Function View File
Process the create form submit and save the nerd to the database. PUT example.com/blogs update() NONE

Controller Function update() app\Http\Controllers\BlogController.php

การลบเรคคอร์ด

Description URL Controller Function View File
Process the create form submit and save the blog to the database. DELETE example.com/blogs/{id} destroy() NONE

Controller Function destroy() app\Http\Controllers\BlogController.php

ในไฟล์

View resources/views/blogs/index.blade.php

ให้แทรกปุ่ม Delete ตามโค๊ดด้านล่าง

 

อ้างอิง
https://scotch.io/tutorials/simple-laravel-crud-with-resource-controllers

https://laravel.com/docs/5.4/controllers#resource-controllers


ป้ายกำกับ: