Laravel 5.8 CRUD (Create Read Update Delete) Generator For Beginners With Example

laravel 5.8 crud tutorial, laravel 5.8 crud example, laravel 5.8 crud generator, Laravel 5.8 CRUD (Create Read Update Delete) Operation For Beginners With Example, CRUD Operations Laravel 5.8Laravel 5.8 CRUD Operation with Example,Create Read Update Delete CRUD Operations Laravel 5.8 with Example

Hello Freinds Today, I will tell you laravel 5.8 crud operation that you can create, read, update and delete how you build in laravel 5.8.

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

Overview

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

Step 1: Laravel 5.8 Install

 

The first step is to install laravel 5.8. To install laravel 5.8 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 5.8. For database configuration, you will need a database name, username, password.
We change the .env file for the database configuration

.env

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 5.8

 

This is the third step. In this step you will be told how to create a table in laravel 5.8, it is very easy to create table in laravel 5.8. The table will create.

php artisan make:migration create_blogs_table --create=blogs

database/migrations

<?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->increments('id');
$table->string('title');
$table->text('content');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('blogs');
}
}

after that Now you have to run this migration by following command:

php artisan migrate

 

Step 4: Create Resource Route in Laravel 5.8

 

routes/web.php

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

 

Step 5: Create Model

 

Then after that we will create a database table model. To create a model we can create a model by using the laravel command. The systax to create the model is given below.

php artisan make:model Blog

 

app/Blog.php

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

 

Step 6: Create Controller

 

Then we will create the controller. Through this controller we will Create, Red, Update, Delete the data.So let’s Create data into laravel 5.8.Learn through this controller.

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([
'title' => 'required',
'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([
'title' => 'required',
'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

 

1. layout.blade.php

2. index.blade.php

3. create.blade.php

4. edit.blade.php

5. show.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 5.8 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

laravel 5.8 We were talking about crud tutorial. This is the second step. In this index page you will show a list of all the blogs, along with the edit, delete and read button will show you on this page. This page also has a button for Create New Blogs at the top. You can click here from the create blog page. And there you can create your own blog.

 

@extends('blogs.layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 5.8 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>Title</th>
<th>Content</th>
<th width="280px">Action</th>
</tr>
@foreach ($blogs as $blog)
<tr>
<td>{{ $blog->title }}</td>
<td>{{ $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

This is the third step of laravel 5.8 crud tutorial. In this file you will get a create blog form. Here you can create your own blog.

 

@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>Title:</strong>
<input type="text" name="title" class="form-control" placeholder="title">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Content:</strong>
<textarea class="form-control" style="height:150px" name="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

This is the fourth step of laravel 5.8 crud Tutorial Generator. With this file you can update your created blog.

 

@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>Title:</strong>
<input type="text" name="title" value="{{ $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>Content:</strong>
<textarea class="form-control" style="height:150px" name="content" placeholder="Detail">{{ $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

This is the fifth step of laravel 5.8 crud Tutorial Generator. With this file you can show and read your created or updated blog.

 

@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>Title:</strong>
{{ $blog->title }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Content:</strong>
{{ $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