Coding

Laravel 5.2 PHP – Build a Shopping Cart – #3 Data Migrations & Seeding

  • 00:00:00 welcome back welcome to a new part in
  • 00:00:03 this level shopping cart video series so
  • 00:00:05 we left when we had this dummy data here
  • 00:00:08 not too bad not too nice I went to
  • 00:00:11 actually seed some dummy data to the
  • 00:00:14 database so that we were at least
  • 00:00:16 working with real data from the database
  • 00:00:18 and then output that database data here
  • 00:00:21 on that view so of course in order to
  • 00:00:24 seed that data
  • 00:00:26 I am only to set up my database and I
  • 00:00:28 will do this using laterals migrations
  • 00:00:31 to create the tables configure the
  • 00:00:32 tables define which fields I want to
  • 00:00:35 have in my tables and also then to seat
  • 00:00:38 the data so in order to do this I'm
  • 00:00:41 navigated into my project folder and I
  • 00:00:44 will run PHP artisan make model to
  • 00:00:46 create a new model just make a little
  • 00:00:50 bit wider so that it fits into one line
  • 00:00:53 and I will give too small a name and I
  • 00:00:56 will name it product since it will be
  • 00:00:58 well a product model the products we see
  • 00:01:00 on the page and I add the EM flag here
  • 00:01:03 to also create a migration file so now
  • 00:01:06 if I save this and go to my project in
  • 00:01:10 the database folder you see the first
  • 00:01:12 you see this new product here in the app
  • 00:01:14 folder that's added to get and in the
  • 00:01:17 database folder in the migrations folder
  • 00:01:19 you see this new migrations file you
  • 00:01:21 also see the user and password migration
  • 00:01:24 fast I will get rid of those I don't
  • 00:01:26 need them here right now and next step
  • 00:01:30 is I want you to configure this schema
  • 00:01:33 for my database table it already has an
  • 00:01:36 ID and the timestamps and that is fine
  • 00:01:38 but I will need a couple of our fields
  • 00:01:40 for one I want to store the path to my
  • 00:01:43 image so this will be a string because I
  • 00:01:46 will store all a part of a string not
  • 00:01:49 the image itself I will name this field
  • 00:01:52 image path I will duplicate this to also
  • 00:01:55 provide a title and duplicate it again
  • 00:01:57 to also provide a description however
  • 00:02:00 for this I will use the text method to
  • 00:02:03 allow for more text and just with a
  • 00:02:05 string which will effectively create a
  • 00:02:07 bar chart which will be limited to a
  • 00:02:09 certain amount of characters 255
  • 00:02:13 by default so with text I allow more
  • 00:02:15 characters and of course the description
  • 00:02:17 should have the chance to be longer and
  • 00:02:19 finally I also need a price this will be
  • 00:02:23 either of it could either be a float I
  • 00:02:27 will stick to integers here but you
  • 00:02:29 could make it a float so then I will
  • 00:02:31 have my price and that is the schema of
  • 00:02:34 my table so all my products will have
  • 00:02:38 these fields in the database and that
  • 00:02:40 sounds about right to me because these
  • 00:02:42 are the fields we have here an image a
  • 00:02:44 title the description and the price in
  • 00:02:47 dollars now I won't manage currencies
  • 00:02:50 here I won't add currency field
  • 00:02:53 everything will be in dollars in this
  • 00:02:54 application you just keep that minimum
  • 00:02:57 simplicity here so if that the schemas
  • 00:03:00 slap next I want to create a seeder for
  • 00:03:03 that I can go back to the terminal and
  • 00:03:06 run PHP artisan make seed and then I
  • 00:03:11 will name this product table cedar now
  • 00:03:17 back in the project folder you will see
  • 00:03:19 a new file in this seats folder and in
  • 00:03:22 this file I can now create the wild DD
  • 00:03:27 initial products I want to have I do
  • 00:03:29 this by creating and you product your
  • 00:03:31 new product and then defining all the
  • 00:03:37 different fields now I could define this
  • 00:03:40 like this product title and then a title
  • 00:03:43 or I can mass assign it however for this
  • 00:03:47 to work I will go into the product model
  • 00:03:49 and I will add a field here the
  • 00:03:53 protected fillable property which is
  • 00:03:57 takes an array or should be an array and
  • 00:04:00 in this array I define all the fields
  • 00:04:02 which I want to be able to assign upon
  • 00:04:05 creation of such an approach a product
  • 00:04:07 object an instance of that class here so
  • 00:04:11 I want to be able to provide the image
  • 00:04:14 path I want to be able loops to set the
  • 00:04:19 title the same is true for the
  • 00:04:21 description and of course for the price
  • 00:04:25 these are the fields I want to be able
  • 00:04:27 to set when creating a new product now I
  • 00:04:30 can just pass an array to this
  • 00:04:32 constructor here and in this array I can
  • 00:04:36 set up the image path which should be
  • 00:04:40 some path the title which will be a
  • 00:04:46 title so Harry Potter for example here
  • 00:04:49 and I will quickly grab the image path
  • 00:04:54 here – oops that's the path also insert
  • 00:05:01 this here and the description supercool
  • 00:05:12 at least as a child haven't read it a
  • 00:05:16 couple of years so I don't know if I
  • 00:05:18 would still like it and the price an
  • 00:05:21 integer so let's say this costs $10
  • 00:05:24 whatever
  • 00:05:25 so then what create went product however
  • 00:05:28 I want to have multiple products of
  • 00:05:30 course and by the way it would create it
  • 00:05:33 because I need to call safe now it would
  • 00:05:35 create however I want to have multiple
  • 00:05:37 products now there are ways to achieve
  • 00:05:40 this I will simply copy that copy that
  • 00:05:45 here and just repeat this process let's
  • 00:05:49 say five times to have five different
  • 00:05:51 books now I won't bore you with filling
  • 00:05:54 this out so I'll be back once I entered
  • 00:05:56 all the data here so I'm done I created
  • 00:05:59 all the seed data in this file next step
  • 00:06:02 is in this database cedar foul I will
  • 00:06:05 specify that I want to call this product
  • 00:06:08 table cedar this will then execute us
  • 00:06:12 once I run the seed command in the
  • 00:06:14 artisan command-line tool and I will do
  • 00:06:16 this now so in order to do this I will
  • 00:06:18 first migrate my migration to create
  • 00:06:21 this database table so run the migrate
  • 00:06:24 command and then I will run PHP artisan
  • 00:06:26 DB seed to see the data and with that I
  • 00:06:31 should have the data in my database and
  • 00:06:33 in fact I can have a look at the
  • 00:06:34 database in my sequel Pro
  • 00:06:38 program here which is connected to my
  • 00:06:39 home state database and here you can see
  • 00:06:41 the product so that looks good so
  • 00:06:43 products are in the database
  • 00:06:45 models is model is created next step is
  • 00:06:49 to actually output them in the view