Laravel 7 and 6.0 CRUD (Create Read Update Delete) Generator For Beginners With Example

Hello Freinds Today, I will tell you laravel 7 & 6.0  CRUD Operation that You Can Create, Read, Update and Delete How you build in Laravel 6.0.

I will tell you a few steps below which the step is to make us crud operation in laravel 6.0.

Keywords :- laravel 7 & 6.0 crud tutorial, laravel 6.0 crud example, Laravel 7 & 6.0 CRUD (Create Read Update Delete) Operation For Beginners With Example, CRUD Operations Laravel 7 & 6.0, Laravel 7 & 6.0 CRUD Operation with Example

Overview

Step 1: Laravel 7 & 6.0 Install
Step 2: Connect Database Configuration
Step 3: Create Table in Laravel 7 & 6.0
Step 4: Create Resource Route in Laravel 7 & 6.0
Step 5: Create Model
Step 6: Create Controller
Step 7: Create Blade Files

Step 1: Laravel 6.0 Install

The first step is to install laravel 6.0. To install laravel 6.0 we first have to install the composer. If you have already installed the composer in your system or laptop. There is no need to install a composer.

composer create-project --prefer-dist laravel/laravel blog

Step 2: Connect Database Configuration

In this step, I will tell you how to do database configuration in Laravel 6.0. For database configuration, you will need a database name, username, password in Laravel 6.0.
We change the .env file for the database configuration

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Database Name 
DB_USERNAME=User Name (root)
DB_PASSWORD=Password

Step 3: Create Table in Laravel 6.0

In this step you will be told how you can create a table using the command of laravel 6.0. The command to create the table below is given.

php artisan make:migration create_blogs_table --create=blogs

database/migrations/tablefoldername.php

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBlogsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('blogs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('blog_title');
$table->text('blog_content');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('blogs');
}
}

Step 4: Create Resource Route in Laravel 6.0

In this step you have to go inside the routes folder and add the resourse url to the file of web.php, for example you can see the code given below.

routes/web.php

Route::resource('blogs','blogController');

Step 5: Create Model

In this step I will tell you how to create a model using the command in laravel 6.0, below is the command to create a model

php artisan make:model Blog

After running the artisan command, a file named Blog.php will be created inside your app folder, you add the following code to that file.

app/Blog.php

<?php 
namespace App;
use Illuminate\Database\Eloquent\Model;
class Blog extends Model
{
protected $fillable = [
'blog_title', 'blog_content'
];
}

Step 6: Create Controller

In this step I will tell you how you can create a controller with the help of artisan command. The command to create controller is given below.

Syntax Create for Controller

php artisan make:controller blogController --resource

After bellow command you will find new file in this path app/Http/Controllers/blogController.php.

1.index()

2.create()

3.store()

4.show()

5.edit()

6.update()

7.destroy()

So, let’s copy code and paste on blogController.php file.

app/Http/Controllers/blogController.php

<?php
namespace App\Http\Controllers; 
use App\Blog;
use Illuminate\Http\Request;
class blogController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$blogs = Blog::orderby('id', 'desc')->get();
return view('blogs.index',compact('blogs'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('blogs.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'blog_title' => 'required',
'blog_content' => 'required',
]);
Blog::create($request->all());
return redirect()->route('blogs.index')
->with('success','blogs created successfully.');
}
/**
* Display the specified resource.
*
* @param \App\Blog $blog
* @return \Illuminate\Http\Response
*/
public function show(Blog $blog)
{
return view('blogs.show',compact('blog'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Blog $blog
* @return \Illuminate\Http\Response
*/
public function edit(Blog $blog)
{
return view('blogs.edit',compact('blog'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Blog $blog
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Blog $blog)
{
$request->validate([
'blog_title' => 'required',
'blog_content' => 'required',
]);
$blog->update($request->all());
return redirect()->route('blogs.index')
->with('success','blogs updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Blog $blog
* @return \Illuminate\Http\Response
*/
public function destroy(Blog $blog)
{
$blog->delete();
return redirect()->route('blogs.index')
->with('success','Blog deleted successfully');
}
}

Step 7: Create Blade Files

Now you create a folder named blogs inside your views folder, then after that create a whole file named blade in that folder.

1. layout.blade.php (resources/views/blogs/layout.blade.php)

2. index.blade.php (resources/views/blogs/index.blade.php)

3. create.blade.php (resources/views/blogs/create.blade.php)

4. edit.blade.php (resources/views/blogs/edit.blade.php)

5. show.blade.php (resources/views/blogs/edit.blade.php)

So let’s just create following file and paste bellow code.

resources/views/blogs/layout.blade.php

<!DOCTYPE html>
<html>
<head>
<title>Laravel 7 & 6.0 CRUD Generator Application</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="container">
@yield('content')
</div>
</body>
</html>

resources/views/blogs/index.blade.php

@extends('blogs.layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 6.0 CRUD Example</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('blogs.create') }}"> Create New Blog</a>
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered">
<tr>
<th>Blog Title</th>
<th>Blog Content</th>
<th width="280px">Action</th>
</tr>
@foreach ($blogs as $blog)
<tr>
<td>{{ $blog->blog_title }}</td>
<td>{{ $blog->blog_content }}</td>
<td>
<form action="{{ route('blogs.destroy',$blog->id) }}" method="POST">
<a class="btn btn-info" href="{{ route('blogs.show',$blog->id) }}">Show</a>
<a class="btn btn-primary" href="{{ route('blogs.edit',$blog->id) }}">Edit</a>
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
@endsection

resources/views/blogs/create.blade.php

@extends('blogs.layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Add New Blogs</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('blogs.index') }}"> Back</a>
</div>
</div>
</div>
@if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('blogs.store') }}" method="POST">
@csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Blog Title:</strong>
<input type="text" name="blog_title" class="form-control" placeholder="title">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Blog Content:</strong>
<textarea class="form-control" name="blog_content" placeholder="Content"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
@endsection

resources/views/blogs/edit.blade.php

@extends('blogs.layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Blogs</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('blogs.index') }}"> Back</a>
</div>
</div>
</div>
@if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('blogs.update',$blog->id) }}" method="POST">
@csrf
@method('PUT')
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Blog Title:</strong>
<input type="text" name="blog_title" value="{{ $blog->blog_title }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Blog Content:</strong>
<textarea class="form-control" name="blog_content" placeholder="Detail">{{ $blog->blog_content }}</textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
@endsection

resources/views/blogs/show.blade.php

@extends('blogs.layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2> Show Blogs</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('blogs.index') }}"> Back</a>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Blog Title:</strong>
{{ $blog->blog_title }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Blog Content:</strong>
{{ $blog->blog_content }}
</div>
</div>
</div>
@endsection

After completing the command, run the following command in your cmd

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/blogs