Coding

Laravel 5.2 PHP Build a social network – Creating Posts (Model & Controller)

  • 00:00:00 hi welcome back and the last video we
  • 00:00:03 prepared this beautiful view here now
  • 00:00:06 let's fill it with some life what I want
  • 00:00:08 to do in this video is I want you enable
  • 00:00:10 a user to post something that will be
  • 00:00:14 the first step so in order to do this we
  • 00:00:17 need a post model or a post model the
  • 00:00:21 first step is to actually create a model
  • 00:00:24 so in my workspace I am in my console
  • 00:00:27 here and I'll do this again with the PHP
  • 00:00:31 artisan command we already use in a
  • 00:00:33 earlier video and what i'm doing here is
  • 00:00:36 i'm running the PHP artisan make model
  • 00:00:39 command and i want to make a model with
  • 00:00:41 the name post this will if you remember
  • 00:00:45 and also relate to a database table
  • 00:00:48 called posts so plural and to create the
  • 00:00:52 micro migration of this database table
  • 00:00:54 i'm entering – m at the end
  • 00:00:57 q yeah creators migration file so now i
  • 00:01:01 hit enter and now you can see in my
  • 00:01:04 migrations folder here I got this create
  • 00:01:08 post table now in this table we already
  • 00:01:12 got the idea at the time stamps field
  • 00:01:16 now what I want to have for my posts to
  • 00:01:19 is I want you have like a text where I
  • 00:01:23 stored the body of the post so to say
  • 00:01:27 and I also want to store an integer and
  • 00:01:32 here the field will be the user ID so
  • 00:01:36 which put a user created this post so
  • 00:01:39 that's all we have a very simple post
  • 00:01:41 model because all we have is really a
  • 00:01:44 body we have no title or tags or
  • 00:01:47 anything like this
  • 00:01:47 so this is enough so that's all I do not
  • 00:01:51 need anymore
  • 00:01:52 but in my but in my post model PHP file
  • 00:01:58 under App the post file here what I will
  • 00:02:02 do is set up the relation to the user
  • 00:02:04 now we haven't talked about relations
  • 00:02:07 yet you are aware that our models a
  • 00:02:11 laravel have kind of a connection to a
  • 00:02:13 database table
  • 00:02:14 the background we already learned that
  • 00:02:16 now database tables have relations and
  • 00:02:20 connections right so we have to have a
  • 00:02:24 possibility to depict that relation in
  • 00:02:27 our PHP file so in the code we write so
  • 00:02:31 that we can access these relations
  • 00:02:33 because like we can create a user really
  • 00:02:37 simple through our user model we might
  • 00:02:40 also want to attach a post to a user now
  • 00:02:42 to be able to do that we need some
  • 00:02:45 chance of accessing this relation
  • 00:02:47 therefore we have to define those
  • 00:02:50 relations in our model files and you
  • 00:02:53 will see how this works in my post file
  • 00:02:57 here in the post model I will create a
  • 00:03:00 new function a public function which I
  • 00:03:03 will call user because each post has one
  • 00:03:06 user who created it
  • 00:03:07 this will take no argument but it will
  • 00:03:11 return something and what it is what the
  • 00:03:14 bowl returned is kind of a query builder
  • 00:03:17 so this will return this and then I
  • 00:03:21 describe the relation in this case a
  • 00:03:25 post belongs to one user so this is a
  • 00:03:28 belongs to relation and here I have to
  • 00:03:32 provide a string as an argument and in
  • 00:03:34 the string I disrupt the path to the
  • 00:03:36 model to which this post model belongs
  • 00:03:39 so the user model in this case and the
  • 00:03:42 path is a p– backslash this is
  • 00:03:44 important use a backslash here user so
  • 00:03:47 this means ok the relation out of the
  • 00:03:51 view or from the view of a post is that
  • 00:03:54 this post belongs to something to a user
  • 00:03:56 now obviously we are going to define the
  • 00:04:00 inverse in the user model here I am also
  • 00:04:03 creating a function which I will call
  • 00:04:08 posts because one user can have several
  • 00:04:12 posts not only one and here I'm also
  • 00:04:15 returning something I'm returning this
  • 00:04:17 and now it's not belongs to because the
  • 00:04:20 user doesn't belong to the post and the
  • 00:04:24 relation is more like one user
  • 00:04:27 has many poets or can have many posts
  • 00:04:31 therefore what we need is this has many
  • 00:04:35 which means it can have several of those
  • 00:04:39 models we're specifying here so this is
  • 00:04:42 a p– post m so that's clear how you set
  • 00:04:47 up these relations that you're defining
  • 00:04:49 how models are connected so in order to
  • 00:04:53 actually create a post what i will do is
  • 00:04:55 i will create a new controller here
  • 00:04:57 which will call post controller i'm just
  • 00:05:03 copying the start of my user control of
  • 00:05:07 file here will rename this to post
  • 00:05:10 control of though here I will use the
  • 00:05:13 the post method I will need to request
  • 00:05:17 but the office set is probably not
  • 00:05:20 something I'll need and then here when
  • 00:05:24 this video we're only creating logic to
  • 00:05:26 create a post therefore I'm creating a
  • 00:05:28 public function post create post this
  • 00:05:36 will be the action or the function will
  • 00:05:38 call through our route now here we will
  • 00:05:41 get request variable and inside of this
  • 00:05:48 function what we will do is we will need
  • 00:05:50 some validation here but that will be
  • 00:05:53 done in the future video I'll just start
  • 00:05:56 with creating the post here and to do
  • 00:05:59 this I will create a new post so a new
  • 00:06:04 post and this post has a body which I
  • 00:06:09 can find in my request and I'll name
  • 00:06:15 that body that field let's see in my
  • 00:06:18 dashboard here I I called it new post
  • 00:06:25 now I like body mayor more mayor German
  • 00:06:29 so yeah that should be called body and
  • 00:06:32 in my post controller this will retrieve
  • 00:06:36 that value and store it in my body
  • 00:06:39 filled with
  • 00:06:41 created here as a text now in order to
  • 00:06:44 save our post we need our user because
  • 00:06:47 we need to create that connection right
  • 00:06:49 now we do this by accessing our user for
  • 00:06:54 a request we can always access the
  • 00:06:56 authenticated user through the request
  • 00:06:59 we're injecting into this function here
  • 00:07:01 so we can access the user there and you
  • 00:07:05 remember we set up this posts relation
  • 00:07:08 in our user model now we can call this
  • 00:07:10 relation here like a function because
  • 00:07:12 well it is a function we set it up as a
  • 00:07:14 function and what this function will do
  • 00:07:16 is it will return as disconnection so
  • 00:07:20 that we can now call the save method on
  • 00:07:22 this and pass the post as an argument
  • 00:07:25 now this will save our post as a related
  • 00:07:30 post to the currently authenticated user
  • 00:07:33 and after that I want to return a
  • 00:07:36 redirect to the dashboard route now let
  • 00:07:45 me run my migrations so that we got our
  • 00:07:49 tables created okay it's now in order
  • 00:07:55 for that to work I need to create a new
  • 00:07:57 route here the route which will get cold
  • 00:08:01 when we click on the submit button this
  • 00:08:04 will be a post route and I will call it
  • 00:08:07 create post or I will give it a URL of
  • 00:08:11 create post and this will use the post
  • 00:08:18 controller add post create post method
  • 00:08:23 there and we'll call it a or it will
  • 00:08:27 give it a name of create or let's say
  • 00:08:31 post create okay now in my dashboard
  • 00:08:36 here I want to include my hidden value a
  • 00:08:44 input field here to pass my my session
  • 00:08:48 code so sure
  • 00:08:52 you think of think of that otherwise
  • 00:08:55 you'll get nasty errors and well you
  • 00:09:00 will be a name will be name will be
  • 00:09:05 token now here I will execute a route
  • 00:09:11 and the route we just created post
  • 00:09:15 create now also add method post here and
  • 00:09:20 now let me see if that works
  • 00:09:21 let me refresh this page here and enter
  • 00:09:25 something well we didn't get an error
  • 00:09:29 now let's have a look into our database
  • 00:09:30 so if we have a look here at our posts
  • 00:09:33 table we see that this new post was
  • 00:09:35 created and attached to a user with the
  • 00:09:37 ID 4 and if you look at this this is
  • 00:09:41 indeed the user arm locked in with
  • 00:09:43 currently so now this is working this is
  • 00:09:46 how we create posts and how we attach
  • 00:09:49 them to a user and level and now in the
  • 00:09:51 next video we will give this a little
  • 00:09:53 bit of fine tuning with validation and
  • 00:09:55 so on see you there
  • 00:09:57 bye