Coding

Laravel 5.2 PHP – Build a Shopping Cart – #15 Displaying Orders in the User Profile

  • 00:00:00 in order to show all the user orders on
  • 00:00:04 this user profile page I'll of course go
  • 00:00:06 to the user profile view year and we
  • 00:00:10 need to make some changes here the first
  • 00:00:12 thing is I'm going to change the width
  • 00:00:14 of my column here a little bit and
  • 00:00:16 thereafter I want you to create a
  • 00:00:19 bootstrap panel here once a bootstrap
  • 00:00:22 panel let's head of a look at bootstrap
  • 00:00:25 components here and then here we got the
  • 00:00:29 panels I want to create a panel with a
  • 00:00:33 footer this one year because in the
  • 00:00:36 content I want to list all the items off
  • 00:00:39 an order and the footer I want to list
  • 00:00:41 the total price so I'm just copying that
  • 00:00:44 code remember I do use bootstrap already
  • 00:00:47 so I can't just copy the code here like
  • 00:00:49 that I also want to add a horizontal
  • 00:00:52 line here and then let's say a heading
  • 00:00:54 where I say your orders something like
  • 00:00:56 that or my orders since it is my profile
  • 00:01:00 page then I have this panel and again
  • 00:01:03 any panel content I want to list the
  • 00:01:05 items of each order back to the
  • 00:01:08 bootstrap page I'll use the list group
  • 00:01:11 class here for that and I can use the
  • 00:01:15 very basic list review with the
  • 00:01:18 unordered list so I'm replacing panel
  • 00:01:20 content with that unordered list get rid
  • 00:01:23 of all list items but one and then of
  • 00:01:25 course I need a couple of for each loops
  • 00:01:27 here now currently I'm not passing
  • 00:01:30 anything to that profile page if we have
  • 00:01:33 a look at the user controller here and
  • 00:01:35 the get profile method well I'm not
  • 00:01:39 passing any data to the user profile
  • 00:01:41 page but I want to pass my user orders
  • 00:01:43 of course so let's work on that in order
  • 00:01:47 to get my orders I'll create a new
  • 00:01:49 variable here and I'll take the
  • 00:01:51 currently logged in user which I know is
  • 00:01:53 available on the profile page because it
  • 00:01:55 is protected with the off mil aware so
  • 00:01:59 I'm using my locked in user and I fetch
  • 00:02:02 all the orders on that user with out
  • 00:02:05 parentheses so I'm accessing this like a
  • 00:02:07 property because I'm already querying
  • 00:02:10 the user here on the database and then I
  • 00:02:12 want to
  • 00:02:13 don't want to build a continued query
  • 00:02:15 instead of just one to take the user
  • 00:02:17 already fetched and then fetch the
  • 00:02:20 orders often user so here I got my
  • 00:02:23 orders and now the next step is I'll use
  • 00:02:26 a built-in method level offers me on
  • 00:02:30 collections collections are allowable
  • 00:02:32 object type kind of and for example all
  • 00:02:36 the data we fetch through eloquent
  • 00:02:39 eloquent again is how we can access
  • 00:02:42 database the om the ORM level ships with
  • 00:02:45 so orders here is a collection and then
  • 00:02:48 we can use the transform method on
  • 00:02:51 orders now what does the transform
  • 00:02:53 method to do the transform method allows
  • 00:02:56 me to kind of edit each order in this
  • 00:03:02 order's collection why do I need to do
  • 00:03:05 that well remember we have that
  • 00:03:08 serialized card in the orders so each
  • 00:03:11 order has this serialized card
  • 00:03:14 I want to unseal eyes that card but I
  • 00:03:18 need to do this for each order now of
  • 00:03:20 course I could also create some loops
  • 00:03:22 and loop through all of them but if the
  • 00:03:24 transform method here it's really easy
  • 00:03:26 the transform method takes an anonymous
  • 00:03:28 function where I have an item this will
  • 00:03:31 be the individual order I could also
  • 00:03:33 name this order to make it clearer and
  • 00:03:36 then the key which will just be 0 1 2
  • 00:03:40 and so on in here I want to take my
  • 00:03:45 individual order an X SD card field
  • 00:03:49 which you know each order has because
  • 00:03:51 I'm saving it like this in the database
  • 00:03:53 so I'm overwriting the card field and
  • 00:03:57 what 1 a well what do I want to enter as
  • 00:04:01 a new value well I want to enter the
  • 00:04:05 uncie realized value of the existing
  • 00:04:07 card field value so the existing card
  • 00:04:11 field or the card field currently at the
  • 00:04:13 beginning holds this long string which
  • 00:04:16 is the serialized version of the PHP
  • 00:04:18 object with the uncie realized method
  • 00:04:21 I'm turning this into an object again
  • 00:04:23 and then chest and well I'm over
  • 00:04:26 the string to no longer be a string but
  • 00:04:29 now be the PHP object again because I'm
  • 00:04:32 in PHP code here again I don't need to
  • 00:04:35 work with a string anymore
  • 00:04:36 and I'm doing that for all the orders in
  • 00:04:39 the well orders collection with the
  • 00:04:41 transform method here so with that I
  • 00:04:44 make sure that the card field actually
  • 00:04:45 holds the PHP object and no strong in a
  • 00:04:48 string anymore I also need to return the
  • 00:04:51 order inside here to well let the
  • 00:04:54 transform method know how the new order
  • 00:04:57 should look like so here I'm changing it
  • 00:04:58 and here I'm just returning it with that
  • 00:05:01 I can pass my orders to my view like
  • 00:05:05 this and then I can work with them
  • 00:05:08 interview of course so in my view I'll
  • 00:05:11 loop through all my orders and I want to
  • 00:05:14 create a new panel for each order
  • 00:05:16 therefore I'm placing the loop outside
  • 00:05:18 of that panel did so here I'll loop
  • 00:05:20 through all my orders as order and I'll
  • 00:05:25 close this loop after this panel div
  • 00:05:28 here and for each like that and inside
  • 00:05:34 the div I do have my unordered list here
  • 00:05:38 where I want to loop through multiple
  • 00:05:40 what multiple items in the cart and the
  • 00:05:44 good thing is I already grouped the
  • 00:05:46 items in the cart because of course I
  • 00:05:48 don't want to print three times
  • 00:05:50 World of Warcraft or anything like that
  • 00:05:52 if I don't have it three times so here
  • 00:05:55 I'll again loop through all my items in
  • 00:05:59 the cart that can access the items while
  • 00:06:02 on my order then on the cart object
  • 00:06:05 which ID serialized so it's a PHP object
  • 00:06:09 again therefore I can also access the
  • 00:06:11 items field on my card and if you have a
  • 00:06:16 look at the cart model again you might
  • 00:06:19 remember that we have this items field
  • 00:06:21 here so with that I'm looping through
  • 00:06:25 all my items as item here and I want you
  • 00:06:31 closely for each loop after
  • 00:06:34 list item here an endless list item
  • 00:06:38 I actually don't only want to output
  • 00:06:41 that text but instead I want to give it
  • 00:06:44 a batch which well tells me the overall
  • 00:06:47 price of this item group so this should
  • 00:06:50 get the batch class and the well you
  • 00:06:56 will just be item price I do have to
  • 00:07:03 access this like this not with the arrow
  • 00:07:05 notation because this is no field on an
  • 00:07:08 item object item is just an associative
  • 00:07:11 array
  • 00:07:11 therefore I'm accessing a field in this
  • 00:07:14 array with the square brackets notation
  • 00:07:16 and I'll place the dollar sign after
  • 00:07:19 that because this will be a price in the
  • 00:07:20 end thereafter
  • 00:07:23 and notice or remember this will be
  • 00:07:25 floated to the right so it will actually
  • 00:07:27 appear on the right even if I entered in
  • 00:07:29 front of the text here at the HTML code
  • 00:07:31 so thereafter I'm outputting the name of
  • 00:07:35 the item so item item and then the name
  • 00:07:41 was stored in the title field like this
  • 00:07:44 and I also want to output quantity I'll
  • 00:07:46 separated with a pipe symbol here you of
  • 00:07:48 course may choose whatever layout you
  • 00:07:50 prefer and here I'll just output item
  • 00:07:55 one to you and then I'll just add units
  • 00:08:00 after it with that the last thing I want
  • 00:08:03 to do is here in the footer the panel
  • 00:08:05 footer should actually get a strong tag
  • 00:08:07 well that's where I say total price and
  • 00:08:10 then output the total price which I can
  • 00:08:13 access on my order
  • 00:08:16 then on the card object and there I had
  • 00:08:18 this total price field which I can
  • 00:08:21 access and again place a dollar sign
  • 00:08:24 well actually in front of it right
  • 00:08:26 that's how we do it so let's do the same
  • 00:08:29 for the individual item price with that
  • 00:08:33 I should output all my orders and if I
  • 00:08:36 go back to my application I am already
  • 00:08:39 logged in if I go to the user profile
  • 00:08:41 again well you see the orders so let's
  • 00:08:44 try this again by making you one
  • 00:08:46 maybe a more complex order where I buy
  • 00:08:49 multiple items like this go to the
  • 00:08:52 shopping cart page click on checkout get
  • 00:08:55 this dummy credit card data again enter
  • 00:08:59 some dummy data up here enter the credit
  • 00:09:02 card enter expiration and so on all the
  • 00:09:05 data click on checkout and thereafter
  • 00:09:08 let's have a look at the user profile
  • 00:09:11 looks pretty good to me with that we got
  • 00:09:15 our working shopping cart application
  • 00:09:18 here if you want to name it like this
  • 00:09:20 and I'm very happy to see you in all the
  • 00:09:23 future videos bye