Tagged: Laravel, Laravel Blog, PHP
- This topic has 0 replies, 1 voice, and was last updated 2 years, 6 months ago by
Chinomnso.
- AuthorPosts
- January 27, 2020 at 5:38 pm #84708Spectator@chinomnso
In the previous article in this series, I wrote about Models and Migrations. And there, it was made clear that each model in Laravel is usually associated with a particular database table, allowing you to manipulate it. Migrations allow you to make changes to an application’s database schema, allowing you to roll it back to a previous version and make structural changes to it.
Manipulating a database table involves creating or inserting data into the table, reading data from the table, updating data in the table, and deleting data from the table. These operations are referred to by the acronym CRUD – CREATE, READ, UPDATE and DELETE or DESTROY. This article gives you an introduction to this concept in Laravel.
To get started, we need to create a controller that will handle our posts. We will call the controller PostController. To get a primer on Controllers, you may wish to read this article. Creating a controller is simple. The command below shows the syntax for doing it. Remember, it is to be done through your terminal, and you have to navigate to your project’s directory first.
php artisan make:controller ControllerName
Running a command with the syntax above will create a controller for you. But since we are creating a controller for handling database-related stuff, we need to add a flag to the command: –resource. Adding that directive tells Laravel that it’s a special kind of controller that’s being created, prompting it to create a boilerplate class with basic methods for CRUD operations. Run the command below to create the controller.
php artisan make:controller PostController --resource
If things go as expected, you should see this message: Controller created successfully. Head over to App/Http/Controllers and you should see the new PostController.php file. Open the file and you should see something like this:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class PostController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { // } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { // } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { // } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { // } }
A mere glance at this would make it clear that this controller is much more advanced than the one we created for our pages. You can take a look again at the controller we created for our static pages and see the differences for yourself. This one comes with a bunch of functions that we can use for data manipulation.
The first method there is index(). It can be used to display a listing of resources. A resource, in this case, can be a blog post. Rather than displaying a single blog post, this method can be used to display a list of blog posts. It can be used on your home page or on any page on which you decide to display a listing of your blog posts.
The next method is the create() method. The name is rather deceptive and might make you think that it is responsible for CREATE, C in CRUD. This method, however, is responsible for displaying a form that creates a new resource. In this case, the form would be one used to create a new blog post.
The store() method handles the storing of resources to the database table. So, when data is collected from the form, it is this method that will store the blog posts to the database table for the posts.
To display a blog post, we will make use of the show() method. Note that we must pass the id of the post we want to display. Hence, $id is passed as an argument to the method. Editing a blog post is done with the help of the edit() method. As with the show method, we need to pass an ID to the method as an argument so it can pick up a specific post and display a form for editing the post.
The update() takes data from the form in which the post editing was done and stores it to the database using the
id
of the post. And the destroy() method is responsible for deleting or destroying posts.The next step is to create routes for each of these functions. We could actually do it the regular way: one line of code for each route. But there is a shorter way of doing it. Add the following line of code to the web.php file in the routes/ directory.
Route::resource('posts', 'PostsController');
If you’ve been following this series, your web.php file should look like this:
Route::get('/', 'StaticController@getHome'); Route::get('about', 'StaticController@getAbout'); Route::get('contact', 'StaticController@getContact'); Route::get('blog', 'StaticController@getBlog'); Route::resource('posts', 'PostController');
The new route we’ve created is not really a route. It’s rather more like a pack of routes. To check for all the routes created in this Laravel application, run the following command in your terminal:
php artisan route:list
You should see something that looks like the following.
You would notice all the routes we created before and the new ones created in our “pack of routes”. At this point, we have created a very important part of our blog. This would be the foundation upon which we would build the major features of our blog application. Subsequently, we would make use of the routes created. We would put some flesh on the methods in the posts controller so that it can carry out the desired functions. Then we would be able to insert posts into the database, read posts from the database, update posts and delete posts.
- AuthorPosts
- You must be logged in to reply to this topic.