Laravel 8 CRUD Example explain here how to make a simple CRUD operation in Laravel 8 step by step. CRUD is an acronym for Create, Read, Update, and Delete. CRUD operations are basic data manipulation for database.
We have explain the Laravel 8 crud example where we have updated in easy and best way to learn create, update, delete, edit with database. Before starting to Laravel 8 crud from scratch for beginner we must need to understand Controller, routes, modal, migrations and theme layouts creation in laravel. If you are already learn all about let’s start with laravel 8 CRUD tutorial step by step process from basic.
Creating CRUD grid is a very common task in web development (CRUD stands for Create/Read/Update/Delete). If you are a senior web developer, you must have created plenty of CRUD grids already. They maybe exist in a content management system, an inventory management system, or accounting software. If you just started web development, you are certainly going to experience lots of CRUD grids’ creation work in your later career. Let’s implement crud in laravel 8 ….
Step 1 : Install Laravel 8
First create new laravel 8 project adding the following command in terminal.
composer create-project --prefer-dist laravel/laravel laravelblog
Step 2: Database Configurations
Now, you need to create the mysql database and connect that database to the Laravel application. Open you localhost/phpmyadmin and add a new database name then add the credentials in .env file which is showing in your project root.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravelblog #your_database_name
DB_USERNAME=root #your_database_username
DB_PASSWORD=admin #your_database_password
Now you can add you tables easily with mysql database.
Step 3: Create Model & Migration
Now generate model and migration file just adding the below command in your terminal which is generate a model file and migration which will show your database table after running the migration command.
php artisan make:model Post -m
Now you can see in your database migration directory created a new file same as below, here you need to add you database columns.
database\migrations\2020_09_13_071913_create_projects_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title')->nullable();
$table->longText('description')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
After adding the columns now run the below command which is generate table in your database.
php artisan migrate
Add Fillable Property: In your app/models inside generated a new file Post.php file where you need to add fillable properties.
app\Models\Post.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'title', 'description'
];
}
Step 5: Add Resource route
In laravel 8 route section have some change here you need to use your controller then initialize the controller class. Open your “routes/web.php” file and your routes same as below.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::resource('posts', PostController::class);
Route::get('/', function () {
return view('welcome');
});
Step 6: Create controller
Now you need to create a controller just adding the below command and generate a new resource controller PostController.
php artisan make:controller PostController --resource
After running this command you will find new file in this path “app/Http/Controllers/PostController.php”. Where you saw here already created methods same like below. Now you need put below codes on your functions.
- index()
- create()
- store()
- show()
- edit()
- update()
- destroy()
Now, open the “PostController.php
” file and just copy past the following code into it.
app/Http/Controllers/PostController.php
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$posts = Post::all();
return view('posts.index', compact('posts'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.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',
'description' => 'required',
]);
Post::create($request->all());
return redirect()->route('posts.index')->with('success','Post created successfully.');
}
/**
* Display the specified resource.
*
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function show(Post $post)
{
return view('posts.show',compact('post'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function edit(Post $post)
{
return view('posts.edit',compact('post'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Post $post)
{
$request->validate([
'title' => 'required',
'description' => 'required',
]);
$post->update($request->all());
return redirect()->route('posts.index')->with('success','Post updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function destroy(Post $post)
{
$post->delete();
return redirect()->route('posts.index')
->with('success','post deleted successfully');
}
}
Step 7: Create Blade Files
Here in this step we create our view files. First create two folder one is “layouts” and another is “posts”. In layout folder create app.blade.php file and and inside posts folder create another blade files. If you don’t know to integrate our blade files in laravel then please check first here how to integration laravel bootstrap admin panel. Now create below blade files for your crud app:
- resources/views/layouts/app.blade.php
- resources/views/posts/index.blade.php
- resources/views/posts/create.blade.php
- resources/views/posts/edit.blade.php
- resources/views/posts/show.blade.php
So let’s just create following file and put bellow code.
resources/views/layouts/app.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Laravel 5.8 CRUD Example Tutorial</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/posts/index.blade.php
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 8 CRUD Example from scratch </h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('posts.create') }}"> Create New Product</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>Description</th>
</tr>
@foreach ($posts as $post)
<tr>
<td>{{ $post->title }}</td>
<td>{{ $post->description }}</td>
<td>
<a class="btn btn-info" href="{{ route('posts.show',$post->id) }}">Show</a>
<a class="btn btn-primary" href="{{ route('posts.edit',$post->id) }}">Edit</a>
<form action="{{ route('posts.destroy',$post->id) }}" method="POST">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
@endsection
resources/views/posts/create.blade.php
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Add New Post</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('posts.index') }}"> Back</a>
</div>
</div>
</div>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('posts.store') }}" method="POST">
@csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</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>Description:</strong>
<textarea class="form-control" style="height:150px" name="description" placeholder="Description"></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/posts/edit.blade.php
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Post</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('posts.index') }}"> Back</a>
</div>
</div>
</div>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('posts.update',$post->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="{{ $post->title }}" class="form-control" placeholder="Title">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Description:</strong>
<textarea class="form-control" style="height:150px" name="description" placeholder="Description">{{ $post->description }}</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/posts/show.blade.php
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2> Show Post</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('posts.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>Name:</strong>
{{ $post->title }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Description:</strong>
{{ $post->description }}
</div>
</div>
</div>
@endsection
Finally, Laravel 8 CRUD Example Tutorial For Beginners From Scratch is over. In this CRUD app we used one function for create and edit. Lets run this crud application example with laravel 7 so run bellow command and see in your browser.
php artisan serve
Now you can open bellow URL on your browser: http://localhost:8000/posts So friends its very easy way to Laravel CRUD operation step by step.
Hope this tutorial help you to learning laravel 8 crud tutorial. If you have any question regarding laravel crud operation generator then please comment below we appreciate your comment.
awesome
i love visit to your web
btw, can you tell me CRUD for multiple table in one form! thx before
Thanks Indra, Can you elaborate, What type crud for multiple table, I did’t understand, Please specify and you can direct mail us on codingdriver15@gmail.com.
Hi, im trying to use this template and get it working for my own database. however i cant seem to find where in this code, the table which is used is specified. could you tell me?
The table for the CRUD is referenced in the model. When you configure your model you will define the table from the database.
Thank you, a quick rundown for a CRUD app.
Btw it should be
@extends(‘layouts.app’)
not
@extends(‘layout.app’)
or just simple change folder name to layout.
Yes, Thanks … code updated
Hello, I did all the steps you showed me but whenever I run ‘php artisan serve’ and go to the website, it keeps showing the laravel welcome page from welcome.blade.php so Im wondering if you could help me. Thanks in advance!
Yes when we setup first the laravel app then its redirect the welcome page, You can access the crud in just ‘/posts’ or you can redirect the page in ‘post.index’ from web.php file. Just replace ‘welcome’ to ‘posts.index’ just like below.
Route::get(‘/’, function () {
return view(‘posts.index’);
});
The
It helped! Thank you so much!
hello can you tell me how to perform crud with a single form without redirection to the next page
yes you can return redirect back methods after storing data
return redirect()->route(‘posts.index’)->with(‘success’,’Post created successfully.’);
Replace it
return back()->with(‘success’,’Post created successfully.’);
or
return redirect()->back()->with(‘success’,’Post created successfully.’);
or
return redirect()->route(‘posts.index’)->with(‘success’,’Post created successfully.’);