Creating a blog is an easy task. Here there is step by step procedure to create a blog in 5 min using Laravel framework.
Create Laravel Project
Create new laravel project using bellow command
$ composer create-project laravel/laravel
Install CRUD Generator
In this example, we are using crudbooster.
install crudbooster
$ composer require crocodicstudio/crudbooster
Add the following class, to “providers” array in the file config/app.php
crocodicstudio\crudbooster\CRUDBoosterServiceProvider::class,
Create and set Up database in .env
DB_DATABASE=**your_db_name** DB_USERNAME=**your_db_user** DB_PASSWORD=**password**
Run the following command at the terminal
php artisan crudbooster:install
Create model and migrate the table
$ php artisan make:model Blog -mc
In your migration add below codes
public function up() { Schema::create('blogs', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('description'); $table->timestamps(); }); }
Now run migration command
$ php artisan migrate
Access the back end /admin/login
- default email : admin@crudbooster.com
- default password : 123456
Create Module for blog
Click on ‘Module Generator’ and select ‘Add New Module’.
fill the form
- Table : blogs
- Module name : Blog
- Module Slug :blog
and navigate to step 2 make necessary changes, then move to step 3, change the type of description to ‘wysiwyg’ from drop down and save module.
Now we can add the new blog posts by clicking Blogs->Add Data
Create page to view Blogs
Here we gonna use Laravel inbuilt layout and pages.
$ php artisan make:auth
remove all the codes from routes/web.php and add the below route
Route::get('/','BlogController@index');
Add the codes in BlogController
/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $blogs = Blog::latest()->paginate(5);; return view('home',compact('blogs')); }
Remove login and registration links from layouts/app.blade.php
Add the following code in home.blade.php
@extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8">@forelse($blogs as $blog) <div class="card"> <div class="card-header">{{$blog->title}}</div> <div class="card-body"><small>{{$blog->created_at}}</small> {!! str_limit($blog->description,40) !!}</div> </div> @empty <p>There is no active post</p> @endforelse</div> </div> </div> @endsection
Our Blog is ready in 5 min