Okay, guys, I think that finally we should implement the full admin post controller, and I think
that now we can appreciate everything we've built and we've learned and see how quickly we can work
once we have implemented and hopefully understood and had fun implementing the base of our system.
So let's just jump to this admin folder inside controllers and create a post controller.
This would be a controller for basically doing the full Crud as it's called, which is creating, reading,
updating and deleting.
Now let me define a namespace here.
That's an app controllers admin.
And let's make the post controller.
So here we've got an Index action.
This would just display the list of all the posts to manage.
The next up, we've got a create one.
That one displays a form.
Next, I'd like to have a store action.
This handles the form submission.
Another one will be called edit and this displays an edit form.
And finally we've got an well not finally.
That's almost the last one.
This is an update.
And by the way it needs an ID, this one as well ID of a post.
This one handles the submission of the edit form, and the final one is delete, which as you can guess,
deletes a post.
Now if for every single Eagle controller, we would try to use this set of methods.
It makes things even easier.
And also we can also think about generating this code automatically, not having to write it every single
time for every specific resource like a post.
So using conventions can really speed up the development, because then you always know what you need
to use, how you need to call things.
And well, you can just generate some of the code.
Okay guys.
So from the top, well I think we're gonna manage to implement every single one of those.
Let's make sure we import the view from the core namespace and let's render something.
So the template should be admin posts index.
The layout is layouts admin and the data.
We've only got posts in here.
We don't need any additional variable.
I think we can just straight away use the post model and get all the posts.
All right, I think we might have a different order here.
So let me use named parameters.
So that's template.
That is layout.
And this one is data.
Now it's fine.
And that's everything.
That's the whole action.
Now obviously you can add more things in here.
You can add pagination.
You can add search.
But I'm going to leave it out to you.
Now let's keep it simple I need to leave you some things you can implement yourself for some practice.
So I think you will be able to do it yourself easily.
Next up we've got the form to add a post.
Well, this is super simple.
I'm just gonna paste the view generation.
It doesn't even have any data.
So that's the whole action.
Now let me implement the edit one.
So for this one we need to display the form.
So the template name will be edit.
And I think we should pass a post in here so we can pre-fill the form here.
Also we're going to skip any variables and just find a model using a specific ID.
See how easy that is.
And finally the delete action.
Well we've got a delete method on a model.
So we can just do it and then use router redirect making sure the router is imported first.
And maybe we can go to admin posts.
There is another problem here and what this is okay.
So this is an instance method not really a static one okay.
No, not a problem.
Let's first find the post using the id and only then let's delete it like this.
Now I also see a potential for improvements here.
For example, in Laravel models also have a method that's called find or fail.
And what this does is it will either return the model or it will immediately return a 404 not found
page if the model wasn't found.
Now, the way we have things implemented, it should be something that you should be able to do by yourself.
So that's something you can do on your own.
And now let's jump back to the store method.
So this one well I think we should first start with authorizing people.
So that's authorization.
So that's not check.
That would have to be verify.
And the action is create post the very first step.
Next up let's collect the data into an array.
And we want the title which will come directly from this post super global title.
And then we've got the content like this and the user ID.
So this you can probably guess how to fetch it.
We've got the auth class, the one from services.
We can get a user and the user ID okay this is all cool.
Now we can use the post model, create to create a new model.
And finally router redirect and just redirect us back to the list of posts.
And there you have it.
Next up.
Let me implement this update one.
So what actions do we have?
Do we have an edit post?
We do and it will require a resource.
So for this one we would need to fetch the post first.
So we use post find ID.
Then we use the edit post action and we pass the post to verify if we can do it or not.
Now the updating works a little different.
We just change the object properties like the title will be set to the post Title.
The post content will be set to post content.
So we don't need this anymore.
And also we save changes differently by calling our post save method.
And then we redirect back to the list of posts.
Now there is also an opportunity for you to add a static update method, which might also accept an
ID and a data array.
Same like the create method works, so you might try to implement that.
If you see this is a good idea I.
This might be just an interesting exercise, but other than that this controller is done.
Everything is implemented.
So the next step would be to add all those views and forms so you can have a completed admin panel.