#27 Create Orders | Laravel Eloquent Relationships | E-Commerce Website in Laravel 5.5
Articles,  Blog

#27 Create Orders | Laravel Eloquent Relationships | E-Commerce Website in Laravel 5.5

okay in this video I will teach you how
we make a model relationship according to our cutaways functionality so for
that I need to two things one is order stable and second is orders model so
first of all let me create migration PHP artisan make migration rate orders table okay migration craters go to the
database migration last one is your new here you can add and Peter and Peter
will be your user ID okay you can add any columns inside your authors paper
but user ID is necessary for now that’s it
let me migrate this PHP artisan migrate okay it’s migrated now create model
create speed art piece and make model auto name orders that’s it
close this open your user PHP here you will create a convenience and with your
new orders table orders models so for that here you will put public function
orders and site here you will say return this this will belongs to many and your class
name is authors plus that’s it now for four Jack is
combination right you will create here test route for just testing here you
will put em you’ll return sound your user okay
and with order now go to your you can close this go to your route test okay
oops okay I forget to add a cat function go
to browser reload again okay abs let’s do the lessons if not found
okay it so does not alter I’m sorry for spelling mistake reload okay its authors
noted this sense of it okay it did others is function name which inside
here belongs to let me try belongs to only alert caught one define English but totally
the lesson others inside the user we have function others belongs to oh sorry
as many if it has many okay yeah here is I’m sorry about that because we need
here has many because it’s right now rotor it’s empty
let me insert a dummy entry for orders we have a user ID so we will put two
orders any user ID like a three three that’s it okay
now you will see inside that three number user three numbers user two
orders here is two orders okay now we will create order with single
relationships motive for that let me check my cart cup is empty we will add
some products inside the cart at one two three let’s say this to time now go to
your cart here is your card cutter a two value for this product one value for
this one now if I click here any checkout it will insert older data so
for that just delete dummy inside the odds our models we will create a
function probably now you know orders and user heavy relationships so we will
complete our order here you will say create order you can add any method for
here yeah you will say what’s the fourth return what use are coming on your
websites so for that mmm put a fuser create a new user that’s it here you
will say check out and here if I say I have a card controller
and there we have it check out function that’s it mmm open your heart controller God controller we will create anywhere
let’s say oblique function here Jacob okay and here you will say return that
combination dual this type we need to so for that you will say it returned your
use here app orders okay and now you can use here orders your function name
create order create order that’s it now it will show error because this is not
static function so we can’t use this type of function it’s God should not be
statically so we will put here there pick now reload both not found you can
use both okay here you will use now here is your open I mean this is your user
data who is logged in on your website now just try to insert the data about
this so for that you will created my own slick it user opers Otis
this one’s this is your dysfunctions okay here you will say create function
simply inside the data now reload it will ins must we insert a data with user
ID in auditable now check your order paper here is your author ID and user ID
that’s it now I need one more for table author of products so for that if I say
I create a new table orders products okay here you will put ID is the
compulsory and also increment okay it will be auto increment
settings do you need Hotel side it’s okay and yeah second is to your its
author products that is products IDs this much to me
opens products okay and int now you need a great credit or you can add timestamp that’s it it will be int save it now you
have to insert same data inside your order products and odors like if I order
some product you will insert row here which user is coming on your website who
click checkout that ID will saved here second you will save here order ID which
one author ID this order ID okay and second is proud
tidy this will be many times this can be many time but order will be single so
for now just delete the order make empty make sure both table now we will first
of all Alba laravel we have a table and Whitfield we have our orders product so
for that we will create a new function call it anything
I call it from quarters column so we will say here we have a some columns but
which column which color belongs to products class products model so here we
will make a relationship with belongs to many with products plus acid ok and here
it will insert order table data and that’s life we will put this one we may
open variable second we need great authors I mean please open place order
and insert back to orders products this will ordered products now we will
say this sorry not this it will orders this variable we are using this belongs
to orders column this function we are making here relationship
these two tables which belong to products actually we are inserting data
to two tables in the same clique same time okay and here we will use attach
that attach contain can be contained attached can have many columns but
product ID is compulsory because we have a hair put product ID order ID will come
from here okay and audre columns will belongs to products so for that we have
to add a products ID mmm that’s nice I think we don’t need created it for now
let me treat this one delete this that’s it older idea product ideas capacity you
can add total quantity text everything but for now is done now if i refresh
this it must be insert data to both table in the same time reload okay
belongs to many undefined method belongs to many K belongs actually belongs the
load method attach that edge I think I have a spelling mistake here so gravel
attach no stunning is not must take case okay here is that mistake we have a
variable we have a function we have a function now reload again here is data
inserted I think here is your author ID 6 product ID 1 so that means we have a
author ID 6 in all the table here is six order ID this is extras we
don’t need this for now delete this so order ID will be single let me if you
confuse let me reload again and make a empty this all okay now reload now you
will see one is product ID order ID 7 check your of the table oh the table
must have a seven ID this will be single this can be multiple
how multiple let me show you you can see we have a card card heavy for product
these types we can add here for that let me make here porridge and haircut
content as we are making a for loop okay and it Carter that’s it and put inside
your placing order this will run multiple time depend on how many product
in your card okay here you will add see data IP simple ID that means you’re
human your product ID that’s it now if I run check out three check out off not
found and so he we use haircut that’s why we define here reload now here is
your products okay is for products is orders is your order now make it empty
again to show you one more thing make it empty now here we need two things one is
quantity second is total so here you will put two things quantity and total
total I think 255 that’s okay that’s fine now how this will go
here you will put array total total will be your cock data a
data want to be multiplied by this is single times that’s why we are using
here price second we need quantity just simply put here see data quantity save
it both tables is empty reload check out again check your order stable here is
order order ID 10 now check inside products order table ten number author
and quantity is 1 1 2 there why let me show you by its tooth – number is
quantity by strawberry and here price is 200 total is 400 year you can see 200
and 400 that means this product price is 200 that’s it your relationship with ya
now if I click on the check out it will go to reload again and destroy your cart
we must empty our cart okay so we will insert check out again that’s why we are
making it again it empty now here we will insert one
more thing total and status inside order table put
here to brutal and spit us both are more work at 255 255 and by default status
will be define binding save it that’s fine
now do this complete you have to say in code the model here you will put array
total here you will put grant order you mean
grand total this one granddaughter okay this will you will insert inside here or
the table okay inside your order table so rocks water it will be multi-pulse
your single product okay list of single product which will show on car but here
it’s single line with grant information grand total grant X grand everything
okay here you will put god total function and you don’t need to add
status okay it’s already done by by default so put a check out click ok open
my god it’s called the fines if I click it it
will go to check out check out here and it will go to your card controller and
we will say click on back and everything is done after that we will put here but
best ROI asteroid funds it will make make back and be reload click on check
out ok is total total is mistaken here cut total hmm I mean I think orders
inserted know the products no let me put here text dummy value 200 now
reload I think I forget something this structure is total and here is quantity and push it I will figure out but there
is something machines know I think here is must take is must be total work is
fight fault not let me save it in this reload oh I am so sorry it’s not
compulsory to null it actually we have to define here actors actor field level
few levels you have to define it which data will be done sales in your other
tables so here you will say Stoker and so sorry it’s here you can change it by
default now if I reload okay and syntax error
decision back side of I think some brother is no must be MC both tables
that’s fine reload no check to your first also table now we
have a order ID 11 and user ID 3 and status is mending and your total is this
chest your card is destroy or not ok card is empty now bears fine and
order products check real order here is just for us that’s fine now let me log
out again login with any other user let me see if is Logan is working just fine
go to product add one two three products any any one this one two times okay
any other yeah let me I think a five products for product one is to quantity
now click on checkout it is this now check here is product ID this for
products and order ID twelve is used need to check toward granddaughter go to
your order here by default pending status created at updated user ID for
this time for who is who is for for is you can check here for is demo human
okay and in the order table you can check everything here you can add more
thing like a text quantity you can everything you can add here just put as
a edit inside here and here that’s it okay
I hope this is understandable and easy to understand
so I will show you more parts in upcoming video if you like my hard work
please hit the like button thank you


Leave a Reply

Your email address will not be published. Required fields are marked *