You are on page 1of 12

Fetch Data From DB

LAB 7
Muhammad Sufyian Mohd Azmi

Copyright by Muhammad Sufyian Mohd Azmi


Database Configuration
 Makesure you already installed wampserver or xampp or laragon.

 Open MySQL and create DB “firstproject” and next create simple table
name it as “users” with 3 attributes (“id”,”name”,”age”) and lastly insert any
data/value into the table.

 Go to DB configuration which is in .env file


 Check on line 13 - 15

Copyright by Muhammad Sufyian Mohd Azmi


Call Data Using Controller
1. Create controller “fetchControl”
2. In “fetchControl” file:-
 Import database class at line 6

 Create function and return DB

Copyright by Muhammad Sufyian Mohd Azmi


Call Data Using Controller
3. Create route for controller

Don’t forget this step (controller name)

URL Controller name Function name in controller

4. Run URL on browser


Copyright by Muhammad Sufyian Mohd Azmi
Fetch Data Using Model and Display
Using View File
 Laravel allowed to automatically connect Model with DB without us to
manually define table name. However, the condition is table name (DB)
and model name MUST be using concept Plurals and Singular. Example:-
 Table Name Model Name
users User
employees Employee

 But what if we do not want our table and model name using Plurals and
Singular, perhaps we want to use Malay word which do not have concept
Plurals or Singular?
 Then we have to tell Laravel manually which table we want to use in a project
(step is done in Model File). Refer last slide
Copyright by Muhammad Sufyian Mohd Azmi
Fetch Data Using Model and Display
Using View File
1. Lets use back the previous DB (refer slide 2).
2. Create a table “customers” in MySQL

3. Create Model file using concept Plurals Singular. So based on the table
name that we created which is customers, so our model name should be
Customer.
4. Syntax to create Model is php artisan make:model <model_name>

Copyright by Muhammad Sufyian Mohd Azmi


-capital alphabet at beginning
-singular name
Fetch Data Using Model and Display Using
View File
5. Create controller

6. Import Model at Controller file


 At line 6 add the syntax

Don’t forget ;

 Lets fetch the data and try to display. Create any function to return data in controller

Function to display all values/data

Copyright by Muhammad Sufyian Mohd Azmi

Model’s name
Fetch Data Using Model and Display
Using View File
7. Create Route for controller

Controller name

URL Controller name Function name

Copyright by Muhammad Sufyian Mohd Azmi


Fetch Data Using Model and Display
Using View File
8. Run on browser and data will be display like this:-

 We do not want the data look like that, we want to display in proper look (in view
[html] file).

Copyright by Muhammad Sufyian Mohd Azmi


Fetch Data Using Model and Display
Using View File
9. Lets modify the controller file so data/value can be display in View file.

Variable that we will use back


View file name in view file (display.blade.php)

Copyright by Muhammad Sufyian Mohd Azmi


Fetch Data Using Model and Display
Using View File
10. Create new file at View and give the file name as display.blade.php

11. Run on browser


Copyright by Muhammad Sufyian Mohd Azmi
Model name and Table name is NOT
using concept Plurals and Singular
 If you do NOT want to use concept Plurals and Singular for your model and
table name (maybe you use Malay word when creating model or table
name), then need to do this step
1. Go to Model file and do this (example your table name is “pelanggan”)

Add this syntax

Copyright by Muhammad Sufyian Mohd Azmi

You might also like