Coding

Laravel 5.2 PHP – Build a Shopping Cart – #8 Cart Model & Session Storage

  • 00:00:00 welcome back everyone so this is what we
  • 00:00:03 finished we have our our product view
  • 00:00:07 here we were able to sign up sign in and
  • 00:00:10 at Sault rate the first thing I want to
  • 00:00:12 do is I want to correct a little mistake
  • 00:00:14 I made in the migrations here in the
  • 00:00:17 user table
  • 00:00:18 I set the user table name to be user it
  • 00:00:21 should be users there with an asset II
  • 00:00:24 and that's important also we'll need
  • 00:00:26 something else here we'll need the
  • 00:00:29 remember token otherwise we'll look at
  • 00:00:33 errors so after doing this make sure to
  • 00:00:36 go to your terminal and run PHP artisan
  • 00:00:39 migrate reset to reset your migrations
  • 00:00:44 and then rerun them with PHP artisan
  • 00:00:46 migrate like that and of course also re
  • 00:00:52 seed your database and after that if you
  • 00:00:55 reload everything everything should look
  • 00:00:57 the same but now the user table name was
  • 00:01:00 updated which is important so that's
  • 00:01:03 that now with that let's get going with
  • 00:01:07 the next part of this series in this
  • 00:01:10 part I actually want to work on the
  • 00:01:13 shopping cart so that we are able to add
  • 00:01:15 an item to the cart and then click on
  • 00:01:18 the shopping cart to see it I will store
  • 00:01:20 everything in the session so I will
  • 00:01:23 store my shopping cart in a session this
  • 00:01:26 has the advantage that it persists if
  • 00:01:28 the user navigates around if the user
  • 00:01:31 leaves the page as long as the session
  • 00:01:32 hasn't expired yet and also it has the
  • 00:01:36 advantage of allowing anonymous users to
  • 00:01:39 well browser items and create a shopping
  • 00:01:41 cart that's of course something other
  • 00:01:43 approaches where we would store our cart
  • 00:01:45 and the database would not be able to
  • 00:01:47 handle that great theme that's why I'm
  • 00:01:49 starting off with this session based
  • 00:01:51 approach now laravel already has session
  • 00:01:55 support both and it makes it really easy
  • 00:01:57 to work with sessions I'll have a look
  • 00:02:00 at the config folder and then the
  • 00:02:01 session file here and we'll just close
  • 00:02:04 some our folders so in here you can set
  • 00:02:08 the default session driver and this is
  • 00:02:11 pointing to the environment file this
  • 00:02:13 dot env
  • 00:02:14 file and then the stop E&V file you can
  • 00:02:18 set up your session driver which by
  • 00:02:19 default is file which means that level
  • 00:02:23 will create a file in the storage app
  • 00:02:26 excuse me storage framework folder and
  • 00:02:29 then here at sessions you see I already
  • 00:02:31 get some files from testing here but I
  • 00:02:33 can't get rid of those and these are the
  • 00:02:36 files where level will store your
  • 00:02:38 session now level has different session
  • 00:02:41 storages and you can learn more by
  • 00:02:43 simply googling for a lateral session
  • 00:02:46 and then checking our tear ducts just
  • 00:02:48 make sure to check them out for at least
  • 00:02:51 version five to two for example and here
  • 00:02:54 you'll see how to configure it and how
  • 00:02:56 to store it so that's something you
  • 00:03:00 might have a look at you'll also learn
  • 00:03:02 it in this sessions PHP file in the
  • 00:03:04 config folder you can set how long a
  • 00:03:06 session should be alive in minutes here
  • 00:03:09 and so on
  • 00:03:11 so that is some important configuration
  • 00:03:13 stuff you should be aware of the default
  • 00:03:15 configuration works for me so I'm not
  • 00:03:17 changing any with anything here and the
  • 00:03:20 first thing I want to start with is that
  • 00:03:23 album what that I'm able to actually add
  • 00:03:26 items to the card let's all head over to
  • 00:03:29 my routes file and I'll set up and
  • 00:03:32 you're out here then you're out I'm
  • 00:03:35 going to create is a get route I'll name
  • 00:03:38 it add to cart and of course give it
  • 00:03:40 whichever path you like however it is
  • 00:03:43 important to note that this should take
  • 00:03:45 a parameter ID because I need to know
  • 00:03:48 which item to add to my cart right then
  • 00:03:51 I'll configure this route to use my
  • 00:03:57 product controller product controller
  • 00:03:59 here and of course get add to cart could
  • 00:04:03 be a good name for this action in the
  • 00:04:06 controller so for the function handling
  • 00:04:07 this and I'll give this route a name as
  • 00:04:10 well and the name I want to give it here
  • 00:04:13 is product add to cart of course choose
  • 00:04:17 whatever works for you here so next in
  • 00:04:20 the product controller I'll add this
  • 00:04:23 functions or add my public function
  • 00:04:25 get Add to Cart and I know that this
  • 00:04:29 will get the idea of the product but
  • 00:04:31 I'll also need access to my requests
  • 00:04:34 instead will store my session so I'm
  • 00:04:35 going to inject it here and by adding it
  • 00:04:38 as a first parameter here level
  • 00:04:40 automatically in checked meters request
  • 00:04:43 object here and then it will also give
  • 00:04:46 me the ID which gets passed through the
  • 00:04:48 route as I set sets up here in my route
  • 00:04:52 path now in this get add to card
  • 00:04:58 function here I of course want to fetch
  • 00:05:01 the product which should get added and
  • 00:05:03 I'll use eloquent to find it by ID like
  • 00:05:07 this so that's really simple to get this
  • 00:05:09 product and next I want you to create or
  • 00:05:13 get it into my session I want to create
  • 00:05:16 a card in my session now I don't just
  • 00:05:19 want to have an array of items in my
  • 00:05:21 session that would be something I don't
  • 00:05:23 like I want to have but here at E is
  • 00:05:26 missing I want to have a real card
  • 00:05:30 object in my session so I'm going to
  • 00:05:33 create a new model in my app folder here
  • 00:05:36 or in this Vianney up folder where I
  • 00:05:39 have a product user model as well now
  • 00:05:41 I'm going to create this by hand and
  • 00:05:43 I'll name it cart dot PHP now this won't
  • 00:05:46 be using eloquent I'm still copying this
  • 00:05:50 just you well have a little bit less to
  • 00:05:53 type but it won't be extending model um
  • 00:05:55 as I said I won't be using Alec went
  • 00:05:58 here right now this is just a normal pH
  • 00:06:03 P class here and this will be the pluie
  • 00:06:08 print I used to create a new card and to
  • 00:06:10 store my items in this card now in this
  • 00:06:14 card I want to have a public field named
  • 00:06:16 items which should hold the individual
  • 00:06:19 products though it's important to note
  • 00:06:21 it with items
  • 00:06:22 I mean group of products so for example
  • 00:06:26 if I were to add Harry Potter and I
  • 00:06:30 added two times then I don't want to
  • 00:06:33 have two Harry Potter items there I want
  • 00:06:36 to have one group of
  • 00:06:37 reporter items and if I then add a Song
  • 00:06:40 of Ice and Fire
  • 00:06:41 a Storm of Swords then I want to have
  • 00:06:44 two items here one is the Harry Potter
  • 00:06:46 group the upper one is DDS Song of Ice
  • 00:06:49 and Fire Storm of Swords group and
  • 00:06:50 within each group I'll then still have
  • 00:06:53 the items and the quality and the price
  • 00:06:56 but I want to group them of course
  • 00:06:58 because I want to present my shopping
  • 00:06:59 cart grouped and I don't need three
  • 00:07:02 times the same item in there I need a
  • 00:07:03 group of those items with the
  • 00:07:05 information of how often it has been
  • 00:07:07 added and so on so that's one thing on
  • 00:07:09 the cart level I also need to have my
  • 00:07:12 total quantity and I'll set this to zero
  • 00:07:14 initially and I want to have my total
  • 00:07:17 price which also should be zero at the
  • 00:07:19 beginning now the important thing about
  • 00:07:23 this cart is I will recreate it each
  • 00:07:26 time
  • 00:07:26 I access it basically whenever I add a
  • 00:07:29 new item or whenever I get the items now
  • 00:07:32 for that of course I need to pass the
  • 00:07:34 old cart to it and then recreate a card
  • 00:07:37 based on that old card I'm following
  • 00:07:40 this approach you always have the
  • 00:07:43 correct state of this card and I'll add
  • 00:07:47 a constructor function for this so that
  • 00:07:50 I'm able to actually pass the old card
  • 00:07:53 to it so in here I'll first check if old
  • 00:07:57 card actually is there is set because if
  • 00:08:00 I'm creating the card for the very first
  • 00:08:02 time I won't have old card of course if
  • 00:08:04 I do have an old card then I will set
  • 00:08:09 items to be equal to old card items and
  • 00:08:14 of course I'll do the same for the total
  • 00:08:18 oops Tito key total quantity if I could
  • 00:08:23 type and for the total price and if I
  • 00:08:28 don't have that well then I will simply
  • 00:08:32 set this items to be null however of
  • 00:08:36 course never way would be to set it to
  • 00:08:39 null here and then get rid of the sell
  • 00:08:41 state but here so that's my constructor
  • 00:08:44 which allows me to recreate this card
  • 00:08:46 based on the old card so that
  • 00:08:48 information gets lost next I'll add a
  • 00:08:53 function which allows me to add a new
  • 00:08:55 item and I want to be able to add a new
  • 00:08:57 item as well as the ID with which this
  • 00:09:00 item should be associated so I will
  • 00:09:03 create a new variable which Illinois
  • 00:09:05 named stored item and here I will just
  • 00:09:08 create a new array of the item I want to
  • 00:09:11 store basically and I'll explain this in
  • 00:09:13 a few seconds again I'll set the
  • 00:09:16 quantity to zero because it will be
  • 00:09:17 incremented in the next step and I'll I
  • 00:09:20 set the price to be equal to item price
  • 00:09:23 and I also set the item itself to be
  • 00:09:28 equal to the item I pass in now stored
  • 00:09:32 item is there for this group I am
  • 00:09:36 referring to so I will check if in the
  • 00:09:41 items I already have in my cart if those
  • 00:09:43 exist or if they are empty so if this is
  • 00:09:46 new card I of course won't have any
  • 00:09:48 items in my card
  • 00:09:49 but if I do have items then I want to
  • 00:09:52 check if I already do have this item I
  • 00:09:56 want to add inside of it I do this with
  • 00:09:58 arrow key exists where I passed the ID
  • 00:10:01 and then the array I want to check for
  • 00:10:05 this so I'll check if I already have
  • 00:10:08 this item and if this is the case then I
  • 00:10:11 will set store item equal to this items
  • 00:10:17 and then retrieve it by the ID so I do
  • 00:10:20 have an associative array here now I'll
  • 00:10:24 add one more line before I'm able to
  • 00:10:26 fully explain all of that I'll then set
  • 00:10:30 my items and here I'm accessing a
  • 00:10:36 certain element by this ID equal to the
  • 00:10:39 stored item so what am I doing here I
  • 00:10:42 add a new item okay and I first
  • 00:10:45 configure this new item to well have a
  • 00:10:48 quantity of 0 the item price and then of
  • 00:10:52 course the item I want to pass now this
  • 00:10:55 is an associative array as you can see
  • 00:10:57 and this associative array will identify
  • 00:11:01 a group of that item so I'm always
  • 00:11:05 overriding the item because I only need
  • 00:11:07 to store it one time this part here I'm
  • 00:11:10 not pushing it on an array or anything
  • 00:11:13 like that I'm not keeping a list of
  • 00:11:15 items because it's always the same item
  • 00:11:17 right so therefore I'm just setting it
  • 00:11:20 here however then I check if I already
  • 00:11:25 have some items in this array or in this
  • 00:11:27 object excuse me in this card and if
  • 00:11:29 this is the case I'll next check if in
  • 00:11:32 all those objects all those products I
  • 00:11:35 already have in my cart I also already
  • 00:11:38 have the item I want to add right now
  • 00:11:41 now I'm passing the ID of the item here
  • 00:11:43 and as well later here in this line set
  • 00:11:47 up an associative array so this items is
  • 00:11:50 basically an associative array where the
  • 00:11:53 key will be the item ID I can check if
  • 00:11:57 this associative array already has the
  • 00:12:00 item ID of the item I want to add as a
  • 00:12:02 key this of course what means that I
  • 00:12:04 already have this item in the shopping
  • 00:12:06 cart in that case that I already do have
  • 00:12:09 the item well then I simply set stored
  • 00:12:13 item equal to the item I already have in
  • 00:12:17 a shopping cart
  • 00:12:18 so I'm overwriting this here basically
  • 00:12:21 this first line I am completely
  • 00:12:23 overwriting it this line here therefore
  • 00:12:25 is used to create a new item if it
  • 00:12:27 hasn't been added and I'm doing this
  • 00:12:29 anyway because I can overwrite it here
  • 00:12:31 if I recognize oh I already have this
  • 00:12:34 item and in this case I have all the
  • 00:12:36 information already in the cart and
  • 00:12:38 that's what I meant before I'm not
  • 00:12:40 pushing this new item on some kind of
  • 00:12:42 array here I'm constantly overwriting it
  • 00:12:45 because I only want to keep each product
  • 00:12:47 once because I only need the information
  • 00:12:49 once and then I only want to increase
  • 00:12:52 the quantity and price appropriately I'm
  • 00:12:54 not doing this yet and worries you're
  • 00:12:56 not missing something I'll add this soon
  • 00:12:59 this line here is then important because
  • 00:13:02 it will access my items which again is
  • 00:13:06 an associative array access to key of
  • 00:13:09 the ID I pass here and store this item
  • 00:13:13 at this position
  • 00:13:15 now either it's simply well overwriting
  • 00:13:19 the existing item so nothing's happening
  • 00:13:23 then if you want to put it like this or
  • 00:13:25 it's creating a new entry with the idea
  • 00:13:27 of the product that passed in case this
  • 00:13:29 product was included on the card before
  • 00:13:31 then it includes then it creates a new
  • 00:13:34 entry with the key of that being that
  • 00:13:38 idea of the past product and stores the
  • 00:13:41 item I set up here so that's what's
  • 00:13:43 happening here and I hope I was able to
  • 00:13:46 clearly explain this now you would be
  • 00:13:49 totally right that this is not working
  • 00:13:51 like this because I need to do some
  • 00:13:53 other things before I pass the stored
  • 00:13:56 item I need to access it
  • 00:13:59 access the quantity field this field I'm
  • 00:14:02 setting up here and increment it because
  • 00:14:06 it's 0 if I created the first time I
  • 00:14:09 certainly want it to be 1 and if I do
  • 00:14:12 have an existing item well then of
  • 00:14:14 course also I want to increase the
  • 00:14:16 quantity because I was adding 1 I don't
  • 00:14:18 just want to do nothing I want to
  • 00:14:20 increase the quantity and I also want to
  • 00:14:22 change the price of course so I'll do
  • 00:14:24 that if I could type here so price
  • 00:14:28 should be the item price times the
  • 00:14:33 stored item quantity so item price is
  • 00:14:38 just the price of a single item and
  • 00:14:40 stored item quantity is simply how many
  • 00:14:44 of such products do we have in the card
  • 00:14:47 yet so let's say we have three times
  • 00:14:49 Harry Potter then this would be free and
  • 00:14:52 since Harry Potter costs 10 this would
  • 00:14:55 be 10 and we would set the stored item
  • 00:14:57 price to fruity and again stored item is
  • 00:15:00 kind of the group of those items then as
  • 00:15:04 explained I'm adding this to my array
  • 00:15:06 here and of course I also need to update
  • 00:15:09 the total quantity and price of my card
  • 00:15:11 so total quantity will go up by one as
  • 00:15:14 well and the total price should also be
  • 00:15:17 increased by this items price
  • 00:15:21 so of the individual item so to stick
  • 00:15:24 with Harry Potter it would be increased
  • 00:15:25 by $10 since that is the price of the
  • 00:15:28 individual book good so that is my card
  • 00:15:32 and that has been a lot of talking but
  • 00:15:35 it is finally prepared and with that
  • 00:15:38 it's time to actually use it so I'll go
  • 00:15:41 back to the product controller to
  • 00:15:44 actually use my card right now I'm only
  • 00:15:48 fetching the product but now I can also
  • 00:15:50 fetch my old card if I do have one so I
  • 00:15:54 will use the session visaed and level
  • 00:15:57 and to do this I have to add use session
  • 00:16:01 at the top here and then I use the has
  • 00:16:06 method to check if well this session has
  • 00:16:09 a card key so if the card has been
  • 00:16:12 stored in this session if this is the
  • 00:16:15 case then I will retrieve it with
  • 00:16:17 session get otherwise I will pass null
  • 00:16:21 so old car sold cards is even null or
  • 00:16:25 the old card then I'll create a new card
  • 00:16:29 and add this use app card import here at
  • 00:16:34 the top and through this new card I'll
  • 00:16:38 pass the old card that's what this card
  • 00:16:41 model is expecting in the constructor
  • 00:16:42 here so now I have the new card and then
  • 00:16:48 I'll call the add method to then add my
  • 00:16:52 product and the product ID and yes of
  • 00:16:55 course you could just add product and
  • 00:16:58 retrieve the ID and D card model but
  • 00:17:01 setting it up this way are more flexible
  • 00:17:03 to also add items which don't have the
  • 00:17:06 ID attached to the individual model for
  • 00:17:08 example then I'll access my request and
  • 00:17:13 here the session this is a different way
  • 00:17:17 and the other way would just be to use
  • 00:17:20 session facets just want to show both
  • 00:17:22 ways here so of course you could also
  • 00:17:25 replace those here with request session
  • 00:17:27 and get rid of the session for Sade I
  • 00:17:29 access my request and then use
  • 00:17:32 here to put the cart back into it or
  • 00:17:38 into it for the first time if we didn't
  • 00:17:40 have it before and of course I want to
  • 00:17:42 Syria serialize D card I just created
  • 00:17:44 and with that I can then return a
  • 00:17:47 redirect request or response here with
  • 00:17:51 the route leading to let's say our
  • 00:17:53 product index which of course if we have
  • 00:17:56 a look at our routes file is our index
  • 00:17:58 page so with that I'm adding items to
  • 00:18:01 the session hopefully at least but we
  • 00:18:04 would be able to see this yet right so
  • 00:18:07 in order to see this out item request
  • 00:18:11 session mmm come on get card here so
  • 00:18:19 this will quit out of my program to see
  • 00:18:22 if this actually works so reload my page
  • 00:18:26 and if I click here nothing will happen
  • 00:18:29 because I have not wired up this link
  • 00:18:31 yet so I'll do that I'll go back here
  • 00:18:34 and on my Add to Cart link I'll add the
  • 00:18:39 route product what did I call it I
  • 00:18:44 called it product Add to Cart so product
  • 00:18:47 Add to Cart and I also need to pass the
  • 00:18:51 ID which of course can access on my
  • 00:18:54 product item here like this so now if I
  • 00:18:57 reload this page if I click Add to Cart
  • 00:19:01 we get to staight and we see our cart
  • 00:19:03 this looks good we have total quantity
  • 00:19:05 of one until price of 10 which fits
  • 00:19:07 Harry Potter we have this items array
  • 00:19:10 where we then have this group I was
  • 00:19:12 referring to with one actually is the ID
  • 00:19:15 of the book here so that's no default
  • 00:19:18 index otherwise it would start at zero
  • 00:19:19 this is the ID of the book we have the
  • 00:19:22 item for the detailed information which
  • 00:19:23 you will need to display title but we
  • 00:19:26 only have at one time and we would have
  • 00:19:27 it only one time if we edit add a two
  • 00:19:30 times and the quantity of this group and
  • 00:19:32 the price so that looks good so I'll go
  • 00:19:35 back and remove this tie damn thing here
  • 00:19:38 to let us work again now of course next
  • 00:19:42 step is to create a shopping cart
  • 00:19:45 and to actually display what's inside
  • 00:19:48 the card inside of our application