Hello friends laravel 6.0 has been released, then through this tutorial I will tell you about the crud operation of laravel 6.0, how you can create, read, update, delete operation application in laravel 6.0.
In laravel 6.0 I will tell you step to step how you can create crud operation. First of all I will tell you overview what step we have to do.
Overview
Step 1: Install Laravel 6.0
Step 2: Database Connect Configuration From .env file
Step 3: Create Table in Laravel 6.0
Step 4: Create Resource Route url in Laravel 6.0
Step 5: Model Create in Laravel 6.0
Step 6: Controller File Create Laravel 6.0
Step 7: Create Blade Files in Laravel 6.0
Step 1:- Install Laravel 6.
In this step, first of all install Laravel 6.0, the command to install Laravel 6 .0 given below.
composer create-project --prefer-dist laravel/laravel blog
Step 2: Database Connect Configuration From .env file
.env
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Database Name Here DB_USERNAME=User Name Here DB_PASSWORD=Password Here
Step 3:- Create Table in Laravel 6
php artisan make:migration create_blogs_table --create=blogs
database/migrations
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 url in Laravel 6
routes/web.php
Route::resource('blogs','blogController');
Step 5: Model Create in Laravel 6
php artisan make:model Blog
App/Blog.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Blog extends Model { protected $fillable = [ 'blog_title', 'blog_content' ]; }
Step 6: Controller File Create Laravel 6
Syntax Create for Controller
php artisan make:controller blogController --resource
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
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 in Laravel 6
- index.blade.php
- create.blade.php
- edit.blade.php
- show.blade.php
- layout.blade.php
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
</pre>
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
resources/views/blogs/layout.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 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>
After completing the command, run the following command in your cmd
php artisan serve
Then open on local browser
http://localhost:8000/blogs