Coding

Laravel 5.2 PHP – Build a Shopping Cart – #4 Outputting Product Data

  • 00:00:00 to output all these products in the view
  • 00:00:03 currently we're of course hard coding
  • 00:00:05 this I will have to loop through them
  • 00:00:08 however I can't loop through all the
  • 00:00:11 products since I need to chant them
  • 00:00:13 because I have several rows here I don't
  • 00:00:17 have all the thumbnails in in one boot
  • 00:00:20 strap row I got multiple rows because I
  • 00:00:22 want to have three items by default if
  • 00:00:25 it's not collapsing on a smaller screen
  • 00:00:27 I want to have three items in one row
  • 00:00:29 therefore I need a way to loop through
  • 00:00:32 chunks of these items to actually well
  • 00:00:36 we'll be able to output what I want you
  • 00:00:39 output here thankfully level makes this
  • 00:00:41 real easy the first step is to start at
  • 00:00:45 the place where we load this view so in
  • 00:00:48 the routes file I will no longer just
  • 00:00:50 return you here
  • 00:00:51 I will add a controller to this whole
  • 00:00:54 application so I will create a new
  • 00:00:57 controller file here can't get rid of
  • 00:00:59 this off folder by the way and now we'll
  • 00:01:01 use a different approach than I did in
  • 00:01:04 my past videos I already showed how you
  • 00:01:06 would create a PHP file and basically
  • 00:01:08 set up like this file here another way
  • 00:01:11 is to use the artist in command-line
  • 00:01:13 tool to run PHP artisan make controller
  • 00:01:17 and you're supplying a name sounds right
  • 00:01:22 so this will be the product controller
  • 00:01:25 if I now have a look at my controllers
  • 00:01:28 file I got this product controller here
  • 00:01:30 and this is just a quick way to create
  • 00:01:32 an empty controller with the port
  • 00:01:35 imports like the request object which
  • 00:01:37 you'll often need already in place so in
  • 00:01:40 you I will create a new controller
  • 00:01:43 action a method in this controller which
  • 00:01:45 I will call get index and then this
  • 00:01:49 function I want to return the view the
  • 00:01:53 same method I was using here in the
  • 00:01:56 routes file and this will use this
  • 00:02:00 controller or allows me to use this
  • 00:02:02 controller to actually use it I will go
  • 00:02:04 back to the routes file and replace this
  • 00:02:06 closure here with an array to configure
  • 00:02:09 this route or to configure it Rick what
  • 00:02:11 request should do or what should happen
  • 00:02:12 with request
  • 00:02:13 reaching this route so I want to use a
  • 00:02:17 controller here the product controller
  • 00:02:20 of course and here d get products get in
  • 00:02:27 excuse me get index action I will also
  • 00:02:32 give this route a name we'll call it
  • 00:02:35 product in X and with that all requests
  • 00:02:40 reaching that route will be redirected
  • 00:02:43 to this controller action here now in
  • 00:02:46 this action I not only want to return
  • 00:02:48 you though this would work you can see
  • 00:02:50 it if I reload I want to return the
  • 00:02:53 different products so I will do this by
  • 00:02:56 creating a new vertical products and
  • 00:02:58 then I will use the product model also
  • 00:03:01 imported here use app product this is
  • 00:03:04 the model we created in the last with
  • 00:03:07 you I will use the small and then the
  • 00:03:11 all method to simply fetch all the
  • 00:03:13 products next I add an array to this new
  • 00:03:18 method here as a second argument to
  • 00:03:20 return all only the view but also attach
  • 00:03:23 some data Tudor's response or not to
  • 00:03:26 this response but to this method called
  • 00:03:29 here where this view is rendered
  • 00:03:31 bilaterals plate templating engine i
  • 00:03:33 want to pass a variable named products
  • 00:03:36 and this name is up to you here this
  • 00:03:38 will create a variable available in the
  • 00:03:41 you PHP file and i will set us equal to
  • 00:03:45 the products variable up here so this
  • 00:03:47 part the string you will create Larimer
  • 00:03:50 will create a new variable called
  • 00:03:52 products which we can then use with
  • 00:03:55 dollar sign products in this view file
  • 00:03:59 in this plate or PHP file so to use it i
  • 00:04:03 want to loop through all the products
  • 00:04:05 but as I said I kind of have to create
  • 00:04:08 some chunks to make sure that actually I
  • 00:04:11 have an outer loop with all the chunks
  • 00:04:15 so for example if I have five products
  • 00:04:17 like it is the case here and I only want
  • 00:04:19 to display free per line I would have
  • 00:04:21 two chunks the first chunk holding free
  • 00:04:24 products and the second chunk holding
  • 00:04:26 the remaining two products
  • 00:04:27 so in order to do this I will create a
  • 00:04:30 new 4-h loop and I will loop through
  • 00:04:32 products as I said this is possible
  • 00:04:35 because this dollar sign products refers
  • 00:04:37 to the variable I create here or I tell
  • 00:04:43 label to create for each products as
  • 00:04:49 product now almost as product chunk
  • 00:04:53 would be a more fitting name because I
  • 00:04:55 want to live for chunks however this
  • 00:04:56 will loop through individual elements
  • 00:04:59 thankfully level offers a useful helper
  • 00:05:02 method here which we can use on any
  • 00:05:04 collection and this is a collection data
  • 00:05:06 you get from database through eloquent
  • 00:05:09 levels o RM will be a collection unless
  • 00:05:14 you got it wire the first method which
  • 00:05:15 will return a single item so this
  • 00:05:17 collection here has this chunk method
  • 00:05:21 which you can use and then I define
  • 00:05:23 chunk size free I want three items in
  • 00:05:26 each chunk and now this will no longer
  • 00:05:28 provide the individual items inside this
  • 00:05:32 loop but instead chunks of items so I
  • 00:05:35 will end this for each loop here and
  • 00:05:38 inside this loop I will copy one row
  • 00:05:42 when bootstrap row here get rid of all
  • 00:05:46 the other HTML killed below here and
  • 00:05:51 also get rid of two columns I only need
  • 00:05:54 one so now I will create this whole
  • 00:05:58 content here per chunk however I only
  • 00:06:02 want to create the rows per chunk
  • 00:06:03 because the columns should be on a per
  • 00:06:05 product basis so inside of this row I
  • 00:06:09 will create another for each loop where
  • 00:06:12 a loop
  • 00:06:13 Rudi's chunk items as products air
  • 00:06:17 because the items in this product chunk
  • 00:06:19 of course are the individual products
  • 00:06:22 just split up over multiple arrays
  • 00:06:25 chunks now I can all do and I should
  • 00:06:29 access this loop and inside of this loop
  • 00:06:33 I'll have my column now that this works
  • 00:06:39 can be seen because if
  • 00:06:41 reload we get five items now so we're
  • 00:06:45 not seeing the data we see that because
  • 00:06:46 I didn't insert this yet but by the fact
  • 00:06:49 that we got five nicely aligned items we
  • 00:06:52 can see that this chunking process here
  • 00:06:55 works now to output the actual data I'll
  • 00:06:59 start with the image I use double curly
  • 00:07:02 braces to enter the level expression
  • 00:07:05 here and then I will simply use my
  • 00:07:07 product and output the image path
  • 00:07:12 because image path is the as one of the
  • 00:07:17 fields I set up and as my creation file
  • 00:07:20 image path here and we can use these
  • 00:07:23 database fields like properties on the
  • 00:07:26 data we get back from the database on
  • 00:07:28 levels since we use again Larimore
  • 00:07:31 eloquent which allows us this mapping
  • 00:07:33 from model of which we work in our
  • 00:07:35 application and database entry which we
  • 00:07:38 have in the backend or in our database
  • 00:07:40 so I can use this now let's just copied
  • 00:07:43 that and do the same for the title and
  • 00:07:48 for the description here and of course
  • 00:07:54 for a price now I will leave the dollar
  • 00:07:56 sign because I don't have that included
  • 00:07:58 in my price I just have a number and
  • 00:08:02 with this if I now reload you can see
  • 00:08:06 that we get a different pictures and
  • 00:08:08 tiles short descriptions and texts now
  • 00:08:10 the thumbnails don't have equal sizes
  • 00:08:13 but that's okay for me and yeah we did a
  • 00:08:17 lot here we are seeing the data we have
  • 00:08:19 in the database we're actually creating
  • 00:08:22 this data we set up the database tables
  • 00:08:24 and now we're outputting it here I'll be
  • 00:08:27 happy to greet you back in the next
  • 00:08:28 videos when we take the next steps see
  • 00:08:30 you there
  • 00:08:31 bye