0% found this document useful (0 votes)
8 views68 pages

Session 3

Uploaded by

giyoo6969
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views68 pages

Session 3

Uploaded by

giyoo6969
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

Laravel CRUD

Controllers
Fundamentals of Laravel Framework

To add resource controller, Open your terminal


and type the command:
php artisan make:controller OrderController --resource --model=Order

• type yes if ask to generate


Fundamentals of Laravel Framework

OrderController:
Fundamentals of Laravel Framework

Model Order:
Fundamentals of Laravel Framework

Connect the Order Model to the


database orders table by adding the
syntax below: Connects to database table orders.

Primary key of the table orders


must be identical

columns of the table orders must


be identical
Fundamentals of Laravel Framework

Open the routes/web.php and add the


syntax:

Connects to OrderController
Fundamentals of Laravel Framework

Auto-Generated views:
Fundamentals of Laravel Framework

In the OrderController, add a return


view for index function:
This will look in the
resources/views
directory for the blade
file:
add_order.blade.php
Fundamentals of Laravel Framework

In the order.blade.php, add a route for


the add order button:
Fundamentals of Laravel Framework
Fundamentals of Laravel Framework

(1) In the OrderController, add a


return view for edit function:
This will look in the
resources/views
directory for the blade
file:
edit_order.blade.php
Fundamentals of Laravel Framework

In the order.blade.php, add a route for


the edit order button: The 2nd parameter will
be changed to the
primary key/ID of each
entry once we finished
the database setup.
Fundamentals of Laravel Framework
Fetching Data from
Database
Fundamentals of Laravel Framework

Open the OrderController.php in


app/Http/Controllers and add the function:
The model Order which connects the
database table orders
all() method get all the
rows from the order table.

Data array values for the


orders
Data array name for the
will go to order.blade.php
orders
Fundamentals of Laravel Framework

Open the routes/web.php and change the


parameters/values of the /order route:
FROM:

REPLACE:

We add the controller index() function we


OrderController previously created in
OrderController
Array brackets to indicate we use a
custom/manual controller function
Fundamentals of Laravel Framework

Open the resources/views/order.blade.php and


replace the row entries with the following syntax:
Fundamentals of Laravel Framework

Fetching the data array values as $order


in the orders data array name.
columns from the orders table.

total = price x quantity

edit() function in the destroy() function in the


OrderController with 1 OrderController with 1
parameter as order id parameter as order id
Fundamentals of Laravel Framework

Open your browser and type the url:


localhost:8000/order

Add another dummy data in the orders table via xamppserver to


check if it also display in our page.
Fundamentals of Laravel Framework

Open your browser and reload the url:


localhost:8000/order
item_name: Coca
Cola
Price: 40
Quantity: 1
Category_id: 2
Created
at/updated_at: date
today
Storing Data to the
Database
Fundamentals of Laravel Framework

Click the Add Order Button


Fundamentals of Laravel Framework

Open the OrderController.php and update the


create() function:
Fundamentals of Laravel Framework

Open the OrderController.php and update the index()


function: The model Category which connects
the database table categories
all() method get all the rows
from the categories table.

Data array values for the


categories
Data array name for the
will go to
categories
add_order.blade.php
Fundamentals of Laravel Framework

Open the resources/views/add_order.blade.php and


replace the entries with the following syntax:
Fundamentals of Laravel Framework

Open the resources/views/add_order.blade.php and


replace the entries with the following syntax:

columns from the categories table

Fetching the data array values as


$category in the categories data array
name.
Fundamentals of Laravel Framework

Open your browser and reload the add order page:


Fundamentals of Laravel Framework

Open the resources/views/add_order.blade.php and add a


route action to the form:
Fundamentals of Laravel Framework

Open the resources/views/add_order.blade.php and add a


route action to the form:

Add also the csrf_field to allow form post request.

Will go to the store function of the OrderController


Fundamentals of Laravel Framework

Open the OrderController in the store function and add


the syntax below:
Fundamentals of Laravel Framework

Open the OrderController in the store function and add


the syntax below: Stores the input fields by
attribute names
Fetches all input fields
A pre-defined method create that saves
the data into the database table orders

Redirect method will transfer a method to include


to another route based on indication of successful
the route name. transaction

Connects to Order Model


which is connected to the
database orders table.
Fundamentals of Laravel Framework

To test the form, Add an entry based on the image


below:
Fetching Specific
Data from the
Database
Fundamentals of Laravel Framework

Open the OrderController and change the edit()


function using the syntax below:
Fundamentals of Laravel Framework

Open the OrderController and change the edit()


function using the syntax below:
A pre-defined method to fetch all data
rows of the category table

Data array objects:


• categories array for the select tags
Connects to Order Model • order array for the input tags
which is connected to the
database orders table.
A pre-defined method to
fetch all data rows of the
Connects to Category Model order table based on the id.
which is connected to the
database categories table.
Fundamentals of Laravel Framework

Open the resources/views/edit_order.blade.php and


replace the entries with the following syntax:
Fundamentals of Laravel Framework

Open the resources/views/edit_order.blade.php and


replace the entries with the following syntax:

A hidden input field to


handle the primary key value
of the order

To allow form POST request

To allow form update


request
Fundamentals of Laravel Framework

(1) Open the resources/views/edit_order.blade.php


and replace the entries with the following syntax:

Inline conditional statement


to compare if the category_id
from the categories is equal
to the category_id of the
orders entry. If equal, it adds
Fetch the data array an attribute selected which
categories object. sets the selected option as
default
Fundamentals of Laravel Framework

(2)Open the resources/views/edit_order.blade.php


and replace the entries with the following syntax:
Fundamentals of Laravel Framework

(2)Open the resources/views/edit_order.blade.php


and replace the entries with the following syntax:

Set the value of the input to


the data array orders object
Fundamentals of Laravel Framework

(3)Open the resources/views/edit_order.blade.php


and replace the entries with the following syntax:
Fundamentals of Laravel Framework

(3)Open the resources/views/edit_order.blade.php


and replace the entries with the following syntax:

Add a route to go back to the


main order page when the
button cancel is clicked.
Fundamentals of Laravel Framework

Open your browser and type the url:


localhost:8000/order and execute edit button
Updating Data to
the Database
Fundamentals of Laravel Framework

Open the OrderController and change the update()


function using the syntax below:
Fundamentals of Laravel Framework

Open the OrderController and change the update()


function using the syntax below: A pre-defined method to
fetch all data rows of the
order table based on the id.

Get all the form fields from


the blade file.
A pre-defined method to
update the entries based on The order id parameter in the
the id. form update route
Form request via POST
Fundamentals of Laravel Framework

Open the resources/views/edit_order.blade.php and


add the action attribute route value with the
following syntax:
Fundamentals of Laravel Framework

Open the resources/views/edit_order.blade.php and


add the action attribute route value with the
following syntax:

Will go the update function in the


OrderController with 1 parameter as the
order id.
Fundamentals of Laravel Framework

Reload the edit_order page and update the fields:

Click Update to check if the data is updated in the


database.
Fundamentals of Laravel Framework
Deleting Data to
the Database
(manual)
Fundamentals of Laravel Framework

Open the OrderController and change the destroy()


function using the syntax below:
Fundamentals of Laravel Framework

Open the OrderController and change the destroy()


function using the syntax below:

A pre-defined method to
delete the entries based on
the id.
Fundamentals of Laravel Framework

Open the routes/web.php and add a delete route to


connect OrderController destroy() function using the
syntax below:
Fundamentals of Laravel Framework

Open the resources/views/order.blade.php and add an


onclick attribute to display a dialog before deleting
action is executed:
Fundamentals of Laravel Framework

Reload the orders page and delete the last entry:


Deleting Data to
the Database
(resource)
Fundamentals of Laravel Framework

Open the routes/web.php and comment out the route


below to disable its functionality:

You may also remove the route instead if we prefer


this deleting method.
Fundamentals of Laravel Framework

Open the resources/views/order.blade.php and add


the syntax below:
Fundamentals of Laravel Framework

Reload the orders page and delete the last entry:


Connecting
multiple database
tables
Fundamentals of Laravel Framework

In the orders page, we need to change the category


value as the category name.
Fundamentals of Laravel Framework

The category_id in the orders table is the primary


key/id of the category table:
orders table:

category table:
Fundamentals of Laravel Framework

Open your app/Models/Order.php and add a category


function:
Fundamentals of Laravel Framework

Open your OrderController.php and replace the query


using the syntax below:
Fundamentals of Laravel Framework

Open your order.blade.php and replace the


$order->category_id with
$order->category->name
Fundamentals of Laravel Framework

Reload the orders page:


CONGRATULATIONS!

You might also like