
بروزرسانی: 31 خرداد 1404
How To Create a Blog in Laravel
Laravel\xa0is a PHP web application framework with an expressive, elegant syntax. It has a vast library of packages and handles much of the drudge work in programming, leaving you to focus on your creativity.
One creative use for Laravel is the construction of a personal blog. This tutorial describes how to use Laravel to build and publish a blog on Kinsta.
For a preview of the project, check out the complete project code.
Prerequisites
To follow this tutorial, ensure you have the following:
- A web server. This tutorial uses XAMPP.
- An account on GitHub, GitLab, or Bitbucket for publishing your application’s code.
- Laravel installed.
- An active MyKinsta account for application hosting. Sign up for a free trial\xa0if you don’t already have one.
Ensure that the Apache and MySQL module services are running in the XAMPP Control Panel. If not, click each service’s Start button in the\xa0Actions\xa0column. Your XAMPP Control Panel should look like this:

By default, MySQL/MariaDB runs on port 3306. Take note of the port if you change it.
If you use a web server other than XAMPP, ensure you are running Apache\xa0or other server software and have installed MariaDB server\xa0on your local machine.
Quickstart With phpMyAdmin
- With MySQL and Apache running, head to your browser.
- Open phpMyAdmin and paste in
http://localhost/phpmyadmin/
. It should display the following:

phpMyAdmin is a database management tool for MySQL and MariaDB.
Create a New Laravel Project
You can now start creating the blog using Laravel. For this tutorial, we used a computer running on Windows.
- Go to your machine’s terminal or command line interface (CLI).
- Create a Laravel project called blog\xa0using the
laravel new blog
\xa0command. - Open your project’s blog\xa0directory with the command
cd blog
. - Then, open the directory in your code editor.
- To check that you built the project successfully, run
php artisan serve
\xa0in your terminal or CMD. - Click the local address output to serve it to the browser. The browser should display the default Laravel Welcome page, shown below:

Configure the Database
Create and configure the database by returning to phpMyAdmin\xa0in your browser and creating a database called blog.
- To create the database, on the Databases\xa0tab, type “blog” in the Create database\xa0field.
- Then, click Create.

- Next, update the database connection to your .env\xa0file at the root of your blog project. Change the
DB_DATABASE
\xa0andDB_PASSWORD
\xa0values to the ones you created.
The connection details should look like this:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=your-db-username
DB_PASSWORD=your-db-password
The other database connection details remain the same as in the .env\xa0file. If you change any connection value, such as changing DB_PORT
\xa0from 3306 to 3307 during configuration, ensure to update it in the .env\xa0file.
Make the Posts Table
Next, create a database model and migrate the changes.
- In your terminal, run
php artisan make:model Post -mc
\xa0to create a model called Post, a table called posts, a migration file, and a controller.

- Check the database/migrations\xa0directory and open the migration file you just created. It has the following format:
YYYY_MM_DD_ID_create_posts_table.php
. - In the
up()
\xa0method of the migration file, create a schema withtitle
,description
, andimage
\xa0attributes.
public function up() {
Schema::create(\'posts\', function (Blueprint $table) {
$table->id();
$table->string(\'title\')->nullable();
$table->text(\'description\')->nullable();
$table->string(\'image\')->nullable();
$table->timestamps();
});
}
- Now, go to your terminal and migrate the changes using
php artisan migrate
, as shown below:

- Go to phpMyAdmin\xa0in your browser, where you will see the posts table:

How To Create Controllers
Adding views and controllers implements your business logic for the database set. The views are the user interfaces that display data objects from the model. Controllers manage the flow of data execution between the model and views.
- Before creating Blade files, run
npm install
, followed bynpm run dev
\xa0in your terminal. The first command installs the required npm packages. The second command starts a Vite development server. - Head to the app/Http/Controllers\xa0directory, open the PostController.php\xa0file, and create an
index
\xa0controller method. The controller method renders a simple text to the browser. To do so, add the following code to thePostController
\xa0class:
public function index() {
$post = "Laravel Tutorial Series One!";
return view(\'posts.index\', [\'post\'=>$post]);
}
This method passes $post
\xa0as a context variable to the views
\xa0section of the index Blade template. $post
\xa0contains text to display, which, here, says, “Laravel Tutorial Series One!” You will replace this with the loop through the posts later.
- Create two new directories in the\xa0resources/views\xa0directory: layouts and posts.
- Inside the layouts directory, create an app.blade.php\xa0file. Other Blade files will inherit from it.
- Copy this code into app.blade.php:
Blog
@yield(\'content\')
By using this HTML code, you import Bootstrap version 5.2.3 and Vite to bundle the JavaScript and CSS assets. The generated page has a header with a navbar and a footer with the scripts called below it. In the body, dynamic content renders from other Blade files with the help of @yield(\'content\')
.
The posts directory holds the Blade files for implementing create and read operations.
- Inside the posts directory, create a Blade file called index.blade.php\xa0and add the following code:
@extends(\'layouts.app\')
@section(\'content\')
@endsection
This code extends from the app.blade.php\xa0file on the layouts page. When rendered in the browser, it shows the content of each blog post and the navigation bar and footer inherited from the app.blade.php file in the layouts\xa0folder. Between the section tags, you pass the content from the controller to render in the browser when you execute the application.
- Set the route in the routes directory. Setting the route allows for automatic loading by the
RouteServiceProvider
\xa0in the App/Providers\xa0directory. TheRouteServiceProvider
\xa0is the class responsible for loading the application’s route files. - Inside the routes/web.php\xa0file, import PostController using
use AppHttpControllersPostController
. - Then, set the route by adding
Route::resource(\'posts\', PostController::class);
\xa0to the routes/web.php\xa0file. - With the Vite development server still running, use
php artisan serve
\xa0to execute the application in your terminal. - With your browser, open
http://127.0.0.1:8000/posts
\xa0to see your new blog post list.
The page should look like the following:

In the next section, we define the controller methods for displaying all posts, creating a post, and storing a post. Then, add their routes and create the Blade files in the corresponding sections.
Create the Blog Post Page
Make blog posts by inputting a title, adding a description, and uploading an image. Then, display your posts in sequential order.
- In the app/Models\xa0directory, open the Post.php\xa0file.
- In the
Post
\xa0class below theuse HasFactory;
\xa0code block, addprotected $fillable = [\'title\', \'description\', \'image\'];
.
This code protects your model attributes from mass assignments.
- In your app/Http/Controllers/PostController.php\xa0file, import the
Post
\xa0model usinguse AppModelsPost;
. - Replace the
index
\xa0andcreate
\xa0controller methods created earlier in thePostController
\xa0class with the code below:
// Show all posts
public function index() {
$posts = Post::orderBy(\'created_at\', \'desc\')->get();
return view(\'posts.index\', [\'posts\' => $posts]);
}
// Create post
public function create() {
return view(\'posts.create\');
}
In the index
\xa0method you just created, the PHP application fetches all posts, puts them in chronological order, and then stores them in a posts
\xa0variable. In the return view, the posts pass into the index.blade.php\xa0file as a context variable in the views/posts\xa0directory. The create
\xa0method returns a create.blade.php\xa0file and places it in the views/posts\xa0directory if a user tries to make a new post.
- Create a
store
\xa0controller method using the code below (to store blog posts in the database). Add this code to thePostController
\xa0class below theindex
\xa0andcreate
\xa0controller methods.
// Store post
public function store(Request $request) {
// validations
$request->validate([
\'title\' => \'required\',
\'description\' => \'required\',
\'image\' => \'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048\',
]);
$post = new Post;
$file_name = time() . \'.\' . request()->image->getClientOriginalExtension();
request()->image->move(public_path(\'images\'), $file_name);
$post->title = $request->title;
$post->description = $request->description;
$post->image = $file_name;
$post->save();
return redirect()->route(\'posts.index\')->with(\'success\', \'Post created successfully.\');
}
The store
\xa0method handles client requests regarding the data in its body, so it takes request
\xa0as an argument. Next, you validate the fields used when creating a post and make a post
\xa0instance from the Post
\xa0model. The inputted field data is then assigned to the created instance and saved. The page redirects to the index
\xa0view with a flash text that says, “Post created successfully.”
Add Routes to Your Posts
To register the routes in your web.php\xa0file:
- In the routes\xa0directory at your project’s root, open the web.php\xa0file.
- Register the routes of the controller methods by replacing the existing code with this:
names([
\'index\' => \'posts.index\',
\'create\' => \'posts.create\',
\'store\' => \'posts.store\',
\'show\' => \'posts.show\',
]);
This controller uses these routes to create, store, and display your data objects.
Make Blade Files
To create the views, return to the PostController
\xa0class:
- In the resources/views/posts\xa0directory, make a Blade file called create.blade.php\xa0and add the code below:
@extends(\'layouts.app\')
@section(\'content\')
@endsection
In this code, create.blade.php\xa0inherits the contents of app.blade.php\xa0in the layouts\xa0directory using @extends(\'layouts.app\')
. These contents include a header, navigation bar, and footer. After adding the Add Post
\xa0text within the h1
\xa0tag, you created a form with the post
\xa0method that contains the {{route(\'posts.store\')}}
\xa0action.
The code enctype="multipart/form-data"
allows for image uploads, and csrf
protects your form from cross-site attacks. Then, the error messages display invalid field entries and usefield attributes
to create labels and inputs for the form.
- Now, replace the code in the index.blade.php\xa0file with the code below to display all the blog posts:
@extends(\'layouts.app\')
@section(\'content\')
@if ($message = Session::get(\'success\')) @endif @if (count($posts) > 0) @foreach ($posts as $post)
image)%7D%7D" alt=""> {{$post->title}}
{{$post->description}}
@endforeach @else No Posts found
@endif
@endsection
This code adds an Add Post\xa0button. When clicked, it creates a post and passes any data into the page’s body. The if
\xa0condition checks if there is data in the database. If there is data, it passes. If not, it displays “No Posts found.”
Structure Your Pages
You can now run your application using php artisan serve
\xa0to create and display blog posts. Open http://127.0.0.1:8000
,\xa0and the page should look like this:

If you add a post, it appears like this:

Deploy Your Laravel Blog to Kinsta
To deploy and test your Laravel application using Kinsta’s Application Hosting service:
- Create a .htaccess\xa0file.
- Push the code to a repository.
- Create a database.
- Set up a project on MyKinsta.
- Build and deploy your blog.
Create an .htaccess File
In the project’s root folder, create a file called .htaccess, and add the following code:
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
This code redirects your application requests to public/index.php\xa0in the deployment.
Push Your Code to a Repository
Create a repository for your project and publish the code. You can use GitHub, GitLab, or Bitbucket to host your code and deploy it to MyKinsta.
Set Up the Database in Your MyKinsta Dashboard
To create a database on MyKinsta:
- Click the Add Service\xa0button and select Database.
- Enter the details of your database as shown below. Note that for your deployment to be successful, you must leave the Database username\xa0as the default value.

The details include the Database name, Display name, Database type,\xa0Version, Database username, Data center location, and Size. This demonstration uses MariaDB for the database, and the Size\xa0is Db3 (1CPU / 4GB RAM / 10GB Disk Space 65 USD / month). You can choose the database type and size that suits your specific needs.
- Click Continue.
- Confirm your monthly cost and payment method, then click Create database.
Set Up the Project on MyKinsta
To deploy your application to MyKinsta:
- Click the Dashboard\xa0panel.
- Click Add Service\xa0and select Application, as shown below:

MyKinsta redirects you to the Add Application\xa0page.
- In the Select branch\xa0card section, select your GitHub repository, then select the Add deployment on commit\xa0checkbox.
- In the Basic details, input the application name and select the data center location for your application.
- Since Laravel needs an app key during deployment, in the Environment variables\xa0card, add an APP_KEY\xa0as Key 1. You can use the APP_KEY\xa0defined in your local .env\xa0file or use an online Laravel Key generator\xa0to get one.
- Click Continue.
- Select the build resources (CPU and RAM) for your application. This demonstration uses the standard build machine (1CPU / 4 GB RAM) – 0.02USD / minute.
- Leave the Set up container image automatically\xa0radio button selected.
- Click Continue.
- In the Set up your processes\xa0page, you can change your application’s pod size and instance by selecting these boxes. This demonstration uses the default values.
- Click Continue.
- Finally, click the Confirm payment method\xa0button to start your application’s deployment. Clicking this also directs you to the Deployment details\xa0page to view the progress of your deployment.
Build and Deploy Your Application
With the database and application hosted, connect the database to your application and build to deploy.
To connect the database, use the external connections of your hosted database. On the Info tab of your hosted database, you see External connections, as shown below:

- On the deployed app’s Settings page, navigate to the\xa0Environment variable\xa0card.
- Click Add environment variable\xa0to add the external connections of your hosted database with the corresponding value. Use the same variables you have in your .env\xa0file.

This screenshot is handy if you consider marking the env
\xa0variables you edited manually to differentiate them from the others.
The APP_URL is the URL of your hosted application, and DB_CONNECTION\xa0is mysql
.
- Head to your application’s Settings\xa0page.
- In the Buildpack card, add PHP and Node.js as build packs. Since this is a PHP application, you must add the PHP build pack last.
- Click Deploy now\xa0to rebuild your application.
Next, add a process that will migrate the database.
- Head to the Processes tab on your hosted application page.
- Select Create process\xa0on the Runtime processes\xa0card.
- Enter Migration\xa0as the name, Background worker\xa0as the type, and
php artisan migrate --force
\xa0as a start command. You can leave the pod size and instances with the default values. - Select Continue to create the process. This action triggers a new build and redeploys the application.
- On the Domains tab of your application, click your application link. You can see that it’s now up and running.
- Note that the blog application deployed to MyKinsta displays no posts. Create a new post by inputting its title, adding a description, and choosing an image.
Summary
Laravel makes it easy to develop a simple blog quickly. Its rapid page loading, robust controller architecture, and competent security make enhancing an application’s performance easy. Meanwhile, MyKinsta lets you release and ship your web applications quickly and effectively. MyKinsta’s flexible pricing model is based on usage, eliminating hidden costs.
When Kinsta hosts your Laravel application, it runs on the Google Cloud Platform on their Premium Tier, making it as fast as possible. Kinsta also includes enterprise-level DDoS protection and mitigation with Cloudflare and advanced firewalls to keep malicious actors at bay and much more.
Start your Application Hosting free trial\xa0right now to streamline your web application development and hosting!