Coding

Laravel 5.2 PHP Build a social network – Outputting Posts

  • 00:00:01 hi everyone welcome back to the next
  • 00:00:03 part of our level series here so in the
  • 00:00:06 last part we fix the validation and in
  • 00:00:09 general we're now able to create posts
  • 00:00:11 and you can see them in the database if
  • 00:00:14 you remember that so if I have a look at
  • 00:00:16 my database here are the posts I already
  • 00:00:18 created well that is good but it would
  • 00:00:23 be nice to actually see them in our or
  • 00:00:26 on our dashboard here we're going to
  • 00:00:28 take care about this in this video so
  • 00:00:31 the place to start is the user
  • 00:00:32 controller here is where we're getting
  • 00:00:35 our dashboard now we might load it in
  • 00:00:38 the user controller but in my opinion it
  • 00:00:40 would better fit in the post controller
  • 00:00:42 because our dashboard is really all
  • 00:00:44 about posts therefore I'm going to cut
  • 00:00:48 this function here and insert it in the
  • 00:00:51 post controller obviously I then have to
  • 00:00:55 adjust my route here in the routes file
  • 00:00:57 where I no longer route to user
  • 00:00:59 controller but instead to the post
  • 00:01:02 controller now this should work again
  • 00:01:05 and we can try this by just reloading
  • 00:01:06 this page yeah so this is working next
  • 00:01:09 thing is I want to be able to render the
  • 00:01:13 real posts here where I have my dummy
  • 00:01:15 posts as a fan I know in order to be
  • 00:01:18 able to do that I need to load or to get
  • 00:01:22 the posts first I do this in a post
  • 00:01:25 controller and in my well.get dashboard
  • 00:01:28 function here before I'm returning a few
  • 00:01:31 I somehow have to fetch my posts now
  • 00:01:35 that is really easy I'll just create a
  • 00:01:37 variable which I could post and then I
  • 00:01:40 will access my post model which I'm
  • 00:01:44 already using in this file here and I
  • 00:01:47 will just call the old method now let me
  • 00:01:50 zoom in a bit
  • 00:01:51 what this old method does on this post
  • 00:01:55 model is well basically fetch all posts
  • 00:01:58 this is how we can build queries through
  • 00:02:01 our our model so to say it was a very
  • 00:02:05 simple query obviously where we just get
  • 00:02:07 well all the elements in the database
  • 00:02:10 now we can create much more complicated
  • 00:02:12 queries and we will see this later in
  • 00:02:14 the series but for now fetching all the
  • 00:02:16 post is just fine
  • 00:02:17 now that I got my posts here I just want
  • 00:02:20 you pass them to the few I do this by
  • 00:02:23 adding a second argument to this return
  • 00:02:25 few function here and the second
  • 00:02:27 argument takes an array now in this
  • 00:02:29 array I'm able to specify all the
  • 00:02:32 variables which I want to pass into the
  • 00:02:34 view so that I'm able to use them in the
  • 00:02:36 view in this case I want to have a posts
  • 00:02:39 variable in the view where it can access
  • 00:02:41 all my posts and what will have pass to
  • 00:02:44 that variable
  • 00:02:44 well my posts variable I got here which
  • 00:02:47 well contains all my posts so now I'm
  • 00:02:51 making the posts I'm fetching here
  • 00:02:52 available in my view therefore in my
  • 00:02:56 view in my dashboard uplight file here
  • 00:02:59 I'm now able to use this posts variable
  • 00:03:02 as if it while were created in this file
  • 00:03:05 here so here where I do have my dummy
  • 00:03:07 posts I will no longer output the stabby
  • 00:03:11 post but I will enter and for each loop
  • 00:03:13 here so at for each and here I will just
  • 00:03:17 loop through all the posts and remember
  • 00:03:19 I said you can use it like a variable we
  • 00:03:21 declared in this file so here just write
  • 00:03:24 dollar sign posts as post to loop
  • 00:03:27 through online posts now it's very
  • 00:03:30 important that this name here dollar
  • 00:03:32 sign posts equals the name you specify
  • 00:03:36 here when creating this variable now
  • 00:03:40 obviously here the dollar sign is
  • 00:03:42 missing that is automatically added
  • 00:03:43 because that as well how variables are
  • 00:03:45 defined in PHP but the name has to be
  • 00:03:49 equal to the one here in the array now
  • 00:03:52 in my dashboard father can now loop
  • 00:03:54 through all those posts and I will close
  • 00:03:57 this for each loop here and inside this
  • 00:04:00 for YouTube
  • 00:04:01 I want to out post output my article
  • 00:04:04 element here so I'm going to get rid of
  • 00:04:06 the second one here which wasn't a me
  • 00:04:08 anyway and now let me save this and
  • 00:04:12 reload the few here now as you can see
  • 00:04:16 we got free dummy articles even though
  • 00:04:19 well we only have one here that is
  • 00:04:22 because the looping works now obviously
  • 00:04:24 we're not seeing our post content
  • 00:04:25 because
  • 00:04:26 all the content here in this article is
  • 00:04:29 still the tamp dummy text but the fact
  • 00:04:32 that we're seeing it three times here
  • 00:04:33 and that we're having free posts in our
  • 00:04:37 database in this case here means that it
  • 00:04:39 is working that it successfully looping
  • 00:04:42 through all this
  • 00:04:43 oops navigate back so it seems to work
  • 00:04:47 that it fetches suppose enter the loop
  • 00:04:49 through the post
  • 00:04:50 now let's output the content of the post
  • 00:04:52 shall we well this is a very easy indeed
  • 00:04:55 here I will just output the body of the
  • 00:04:58 post so let me just have a look at my
  • 00:05:01 migration file I think I call it body
  • 00:05:03 yeah so therefore I will enter my blade
  • 00:05:07 expression here with double curly braces
  • 00:05:09 and then just exit post body so
  • 00:05:12 accessing the database field body like a
  • 00:05:16 property of well the post object this is
  • 00:05:19 all the magic level tasks for us we can
  • 00:05:21 access data at this easy so I think that
  • 00:05:24 is really awesome and I might also
  • 00:05:26 access the offer and now back in our
  • 00:05:28 database
  • 00:05:29 remember we got our posts here and our
  • 00:05:31 users in a number table so I want to
  • 00:05:33 output the first name that I specified
  • 00:05:35 here now to be able to do that I will
  • 00:05:40 again enter my blade expression and then
  • 00:05:43 post but now what in my posts table here
  • 00:05:47 I don't have the offer name I just have
  • 00:05:50 the ID of the user now we set up this
  • 00:05:53 relation and therefore I can access my
  • 00:05:56 users or my user like a property over
  • 00:06:02 this post this user property here is
  • 00:06:06 defined in our post PHP file in the
  • 00:06:09 model file here here we're creating this
  • 00:06:12 user function and we can access this
  • 00:06:16 well the same name like a property to
  • 00:06:19 fetch the actual user so not use we're
  • 00:06:22 not using the function here which would
  • 00:06:24 hand us back part of the query builder
  • 00:06:26 which would allow us to query the
  • 00:06:28 database and make a more complex
  • 00:06:30 transaction but instead we're directly
  • 00:06:33 querying the user which is stored as a
  • 00:06:35 property of or opposed also
  • 00:06:38 automatically by level
  • 00:06:39 therefore I can't just XC user here and
  • 00:06:43 then access well the first name field
  • 00:06:45 let me save this and reload and as you
  • 00:06:49 can see this is working I'm now seeing
  • 00:06:51 all my post bodies here and those happen
  • 00:06:54 to be test tests and another test and
  • 00:06:57 post by Max now I've have a look at my
  • 00:07:00 database we see all posts were created
  • 00:07:02 by user with the ID 4 and if I look in
  • 00:07:05 the users table well we see that 4 is
  • 00:07:08 indeed max now the date is obviously
  • 00:07:11 still wrong so let's fix that I can
  • 00:07:14 output this in my blade expression your
  • 00:07:17 cube by just accessing post created at
  • 00:07:20 save this and reload and now we're
  • 00:07:23 seeing the date now this is a bit ugly
  • 00:07:25 formatted right and you can reformat
  • 00:07:28 that but I'm not going to focus on that
  • 00:07:30 right now the main point is that we're
  • 00:07:32 now successfully outputting all our
  • 00:07:34 posts with all the data we have attached
  • 00:07:37 to your post and you saw how easy that
  • 00:07:40 was right so that is a really powerful
  • 00:07:42 thing we got here going and we will
  • 00:07:45 continue in the next video with being
  • 00:07:47 able to well delete post through these
  • 00:07:50 buttons here
  • 00:07:51 see you there bye