WEBVTT

00:01.220 --> 00:11.510
Okay, guys, I think that finally we should implement the full admin post controller, and I think

00:11.510 --> 00:19.010
that now we can appreciate everything we've built and we've learned and see how quickly we can work

00:19.010 --> 00:27.860
once we have implemented and hopefully understood and had fun implementing the base of our system.

00:28.220 --> 00:36.080
So let's just jump to this admin folder inside controllers and create a post controller.

00:36.530 --> 00:45.590
This would be a controller for basically doing the full Crud as it's called, which is creating, reading,

00:45.620 --> 00:47.570
updating and deleting.

00:47.840 --> 00:50.390
Now let me define a namespace here.

00:50.420 --> 00:54.080
That's an app controllers admin.

00:55.010 --> 00:58.640
And let's make the post controller.

00:59.630 --> 01:03.740
So here we've got an Index action.

01:03.740 --> 01:08.180
This would just display the list of all the posts to manage.

01:08.600 --> 01:12.470
The next up, we've got a create one.

01:13.790 --> 01:16.220
That one displays a form.

01:16.820 --> 01:21.800
Next, I'd like to have a store action.

01:21.830 --> 01:24.740
This handles the form submission.

01:25.250 --> 01:32.960
Another one will be called edit and this displays an edit form.

01:32.960 --> 01:37.340
And finally we've got an well not finally.

01:37.340 --> 01:39.560
That's almost the last one.

01:40.190 --> 01:41.600
This is an update.

01:41.600 --> 01:46.490
And by the way it needs an ID, this one as well ID of a post.

01:46.610 --> 01:58.970
This one handles the submission of the edit form, and the final one is delete, which as you can guess,

01:58.970 --> 02:00.680
deletes a post.

02:00.680 --> 02:07.550
Now if for every single Eagle controller, we would try to use this set of methods.

02:07.580 --> 02:10.100
It makes things even easier.

02:10.100 --> 02:18.410
And also we can also think about generating this code automatically, not having to write it every single

02:18.410 --> 02:22.460
time for every specific resource like a post.

02:22.610 --> 02:29.990
So using conventions can really speed up the development, because then you always know what you need

02:29.990 --> 02:32.030
to use, how you need to call things.

02:32.030 --> 02:35.720
And well, you can just generate some of the code.

02:37.010 --> 02:37.610
Okay guys.

02:37.610 --> 02:43.550
So from the top, well I think we're gonna manage to implement every single one of those.

02:43.550 --> 02:50.390
Let's make sure we import the view from the core namespace and let's render something.

02:50.390 --> 02:55.190
So the template should be admin posts index.

02:55.400 --> 03:00.860
The layout is layouts admin and the data.

03:00.860 --> 03:02.840
We've only got posts in here.

03:02.840 --> 03:05.570
We don't need any additional variable.

03:05.570 --> 03:12.500
I think we can just straight away use the post model and get all the posts.

03:12.860 --> 03:15.590
All right, I think we might have a different order here.

03:15.590 --> 03:17.720
So let me use named parameters.

03:17.720 --> 03:19.880
So that's template.

03:19.910 --> 03:21.410
That is layout.

03:21.410 --> 03:23.660
And this one is data.

03:23.690 --> 03:24.710
Now it's fine.

03:24.710 --> 03:25.850
And that's everything.

03:25.850 --> 03:27.170
That's the whole action.

03:27.170 --> 03:30.410
Now obviously you can add more things in here.

03:30.410 --> 03:32.660
You can add pagination.

03:32.840 --> 03:35.120
You can add search.

03:35.120 --> 03:37.400
But I'm going to leave it out to you.

03:39.350 --> 03:45.440
Now let's keep it simple I need to leave you some things you can implement yourself for some practice.

03:45.440 --> 03:49.130
So I think you will be able to do it yourself easily.

03:49.190 --> 03:54.830
Next up we've got the form to add a post.

03:55.100 --> 03:57.020
Well, this is super simple.

03:57.020 --> 04:00.350
I'm just gonna paste the view generation.

04:00.350 --> 04:02.780
It doesn't even have any data.

04:02.810 --> 04:04.490
So that's the whole action.

04:04.940 --> 04:07.190
Now let me implement the edit one.

04:07.190 --> 04:11.510
So for this one we need to display the form.

04:12.020 --> 04:14.990
So the template name will be edit.

04:14.990 --> 04:21.320
And I think we should pass a post in here so we can pre-fill the form here.

04:21.320 --> 04:27.950
Also we're going to skip any variables and just find a model using a specific ID.

04:27.980 --> 04:30.740
See how easy that is.

04:30.980 --> 04:34.040
And finally the delete action.

04:34.040 --> 04:38.810
Well we've got a delete method on a model.

04:38.810 --> 04:50.750
So we can just do it and then use router redirect making sure the router is imported first.

04:50.750 --> 04:54.560
And maybe we can go to admin posts.

04:56.090 --> 05:00.770
There is another problem here and what this is okay.

05:00.800 --> 05:07.610
So this is an instance method not really a static one okay.

05:07.650 --> 05:09.450
No, not a problem.

05:09.450 --> 05:20.490
Let's first find the post using the id and only then let's delete it like this.

05:22.380 --> 05:25.380
Now I also see a potential for improvements here.

05:25.380 --> 05:31.260
For example, in Laravel models also have a method that's called find or fail.

05:31.290 --> 05:39.750
And what this does is it will either return the model or it will immediately return a 404 not found

05:39.750 --> 05:42.180
page if the model wasn't found.

05:42.210 --> 05:54.030
Now, the way we have things implemented, it should be something that you should be able to do by yourself.

05:55.320 --> 05:57.990
So that's something you can do on your own.

05:58.500 --> 06:03.690
And now let's jump back to the store method.

06:04.170 --> 06:10.920
So this one well I think we should first start with authorizing people.

06:13.710 --> 06:16.590
So that's authorization.

06:16.590 --> 06:18.150
So that's not check.

06:18.150 --> 06:20.880
That would have to be verify.

06:20.880 --> 06:26.310
And the action is create post the very first step.

06:26.460 --> 06:30.360
Next up let's collect the data into an array.

06:30.690 --> 06:40.170
And we want the title which will come directly from this post super global title.

06:40.650 --> 06:49.980
And then we've got the content like this and the user ID.

06:50.370 --> 06:53.910
So this you can probably guess how to fetch it.

06:53.940 --> 06:57.720
We've got the auth class, the one from services.

06:57.720 --> 07:04.560
We can get a user and the user ID okay this is all cool.

07:04.560 --> 07:09.750
Now we can use the post model, create to create a new model.

07:09.750 --> 07:19.320
And finally router redirect and just redirect us back to the list of posts.

07:19.320 --> 07:21.840
And there you have it.

07:23.640 --> 07:25.110
Next up.

07:28.020 --> 07:31.020
Let me implement this update one.

07:31.020 --> 07:33.480
So what actions do we have?

07:33.510 --> 07:36.060
Do we have an edit post?

07:36.090 --> 07:40.860
We do and it will require a resource.

07:41.850 --> 07:47.520
So for this one we would need to fetch the post first.

07:47.520 --> 07:50.970
So we use post find ID.

07:51.960 --> 08:02.340
Then we use the edit post action and we pass the post to verify if we can do it or not.

08:03.150 --> 08:05.460
Now the updating works a little different.

08:05.460 --> 08:13.230
We just change the object properties like the title will be set to the post Title.

08:13.260 --> 08:20.400
The post content will be set to post content.

08:20.580 --> 08:22.500
So we don't need this anymore.

08:22.500 --> 08:30.060
And also we save changes differently by calling our post save method.

08:30.060 --> 08:33.630
And then we redirect back to the list of posts.

08:33.660 --> 08:42.180
Now there is also an opportunity for you to add a static update method, which might also accept an

08:42.180 --> 08:45.030
ID and a data array.

08:45.060 --> 08:51.030
Same like the create method works, so you might try to implement that.

08:51.030 --> 08:53.670
If you see this is a good idea I.

08:53.700 --> 09:01.260
This might be just an interesting exercise, but other than that this controller is done.

09:01.260 --> 09:02.790
Everything is implemented.

09:02.790 --> 09:13.020
So the next step would be to add all those views and forms so you can have a completed admin panel.
