Laravel 6 Tutorial for Beginners #19 - Post Requests
Skills:
Backend Performance60%
Key Takeaways
Handles POST requests in Laravel to save data to a database
Full Transcript
alright then so we've created this form so we user can add a new pizza but at the minute if they add in some data choose a type across and press on submits then nothing happens and that's because we're not handling this post request so what we ultimately need to do is set up a route to handle that post request now at the minute inside our forum we've said that the action is forward slash pizzas that means that we're going to send this post request to this endpoints so just going back to our naming conventions for this Pizza model I've added on another line so this request is going to be a post request and the route is going to be forward slash pizzas now this is the same as the get request the get request for forward slash pizzas uses the index action and that grabs all of the data from our pizzas table and it lists it on an index of view but this time when we send a post request and send it to this route we're not going to go to the database and grab it more stuff instead inside a store action inside the Pizza controller again a naming convention what we're going to do is take the data from the form that we get from the post request and we're going to save it to the database now we don't need a view for this because at the end of the day we're not then returning a view for this post request what we'll probably do is take the data save the data to the database and then redirect them to maybe this route or I don't know the welcome screen so that's what we're going to do in this video we're going to create this action right here the store action on the Pizza controller and we're going to set up this route handle app which invokes that so we can save it to the database so then back in our routes file what I'm gonna do is place another route right here so route double colon oops spell it correctly and this is going to be a post route this time so not get post because we're accepting a post request and the route is going to be forward slash pizzas and then we need to specify the pizza controller right here and say what action we want to use it's going to be an action called store because at the end of the day we're storing something in the database ultimately so now we've created that we can go ahead and create this action inside the Pete's controller so let's do that I'm going to say down here public and it's a function called store and then inside here ultimately we're going to save something to the database we're not going to do that just yet instead what I'm going to do is just return a redirect so return redirect and then we'll say forward slash so when we submit the form at the minute the only thing that would happen is we would take the data not really do anything with it and then just redirect to the home screen you know the welcome for you so if I save this and try it out there is going to be a problem and you'll see that now let me just refresh first of all do this and then say order pizza now we get error 419 page expired and that's because we've missed something else from our form and this error right here is actually built into laravel to help protect from cross-site request forgery and that's the type of potential attack on a server so to get around this all we need to do is come to our form right here and then inside the form we need to use a blade directive and that is C s RF cross-site request forgery okay so now if we say this this should work so let me go back to the form I'm gonna refresh and add something in say order pizza and it redirects us back to the welcome screen okay so that's all working but at the minute we're not accessing the data which we take in right here so how do we do that well it's pretty simple if we go to the controller right here we get access to the data on the request so we can say request and then whatever bit of data we want so I could say name and that gets us whatever a user has typed into the field with the name attribute of name does that make sense so if we go back now we could actually log this to the terminal down here by using a function and that function is called error underscore and I'm just gonna log this down here so now when we submit the form we're going to grab that bit of data from the name field log it down here and then we're going to redirect now we need to go to the terminal where we're actually running the local development server so save it and let's go back over here I'm going to refresh on the form and I'm going to type in Shawn and then order pizza so if I go back over here we can see that now Shawn has been logged down here to the terminal so cool let's do the same thing with the other fields so I could grab the type and remember that was the name attributes of this thing right here so whatever a user selects that's going to be the type and then finally we have the base as well right here so let's go over here and do base like so save that and refresh or rather go order a pizza I'm gonna type in Yoshi and then this is going to be volcano and thin crispy order pizza would get redirected and you can see over here all of that data is locked so now we're accessing that data and instead of just logging it down here at this point what we'd probably want to do is save that data as a record in the database and we'll do that in the next video
Original Description
Hey all, in this Laravel 6 tutorial we'll talk about POST requests and how we can handle them in Laravel.
🐱👤🐱👤 JOIN THE GANG -
https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join
----------------------------------------
🐱💻 🐱💻 My Udemy Courses:
+ Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript
+ Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase
+ D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase
🐱💻 🐱💻 Course Links:
https://github.com/iamshaunjp/laravel-beginners-tutorial/tree/lesson-19
🐱💻 🐱💻 Other Related Free Courses:
+ PHP & MySQL for Beginners - https://www.youtube.com/playlist?list=PL4cUxeGkcC9gksOX3Kd9KPo-O68ncT05o
+ Object Oriented PHP - https://www.youtube.com/playlist?list=PL4cUxeGkcC9hNpT-yVAYxNWOmxjxL51Hy
🐱💻 🐱💻 The Net Ninja Community Boards:
https://community.thenetninja.co.uk/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: Backend Performance
View skill →Related Reads
📰
📰
📰
📰
Inside the Wayfair Frontend SDE-2 Interview: A Complete Breakdown
Medium · Programming
I Spent Two Years Maintaining a React SPA. HTMX Rebuilt It in a Week
Medium · Programming
The 5 Levels of Front End Engineering (And Where Most Developers Get Stuck)
Medium · Programming
Browser-Based PDF Editing with Vue 3 and pdf-lib
Dev.to · sunshey
🎓
Tutor Explanation
DeepCamp AI