Hi guys, In this tutorial we will learn Laravel 8 CRUD Full Example For Beginners. CRUD means create read update and delete item for over laravel 8 web applictios.
CRUD (create read update and delete) is a most importat part of web applictions, so we will help you to give example of crud operation in laravel 8.
Laravel is a best powerful web application framework, So you will learn very basic crud operation with laravel new version 8.
so we will following step by step.
- Step : Install Laravel 8
- Step: Database Configuration
- Step: Create Migration
- Step: Create Resource Route
- Step: Create Controller And Model
- Step: Create View Files
Step 1 : Install Laravel 8
Now we need laravel 8 fresh version applicatio if you have no, so So open your terminal OR command prompt and run bellow command.
composer create-project --prefer-dist laravel/laravel laravel-crud
Step 2: Database Configuration
Now next step, in this step we need connect database for laravel 8 application, so go to inside the application, then open .env file and edit following code.
DB_CONNECTION=mysql
DB_HOST=128.0.0.1
DB_PORT=3306
DB_DATABASE=laravel-crud
DB_USERNAME=root
DB_PASSWORD=*********
Step 3: Create Migration
After Connect databse we need to student table, Now we create Migration and then create table, which is the following command.
php artisan make:migration create_posts_table --create=posts
After run above command created one file for under database/migrations/. so go to database/migrations/ and edit student table file, which is the following code.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('detail');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
After Edit create_posts_table file you have to run this migration by following command.
php artisan migrate
The above command has created a posts table inside the database.
Step 4: Create Resource Route
Now next we need route so open your "routes/web.php" file and add following route.
Route::resource('posts','PostController');
After Create Resource Route we have a 8 route for laravel 8 applictions, so following bellow table.
Method URI Name Action
| | GET|HEAD | posts | posts.index | App\Http\Controllers\PostController@index | web |
| | POST | posts | posts.store | App\Http\Controllers\PostController@store | web |
| | GET|HEAD | posts/create | posts.create | App\Http\Controllers\PostController@create | web |
| | GET|HEAD | posts/{post} | posts.show | App\Http\Controllers\PostController@show | web |
| | PUT|PATCH | posts/{post} | posts.update | App\Http\Controllers\PostController@update | web |
| | DELETE | posts/{post} | posts.destroy | App\Http\Controllers\PostController@destroy | web |
| | GET|HEAD | posts/{post}/edit | posts.edit | App\Http\Controllers\PostController@edit | web
Step 5: Create Controller And Model
Now in this step we run the resource controller and model command. In this command create controller and model automatically, which is the following command.
php artisan make:controller PostController --resource --model=Post
After run bellow command we have 2 new file controller and model
- app/Http/Controllers/PostController.php
- app/Models/Post.php
Now go to app/Http/Controllers/PostController.php file and In this controller will create seven methods by default as bellow methods:
- index
- create
- store
- show
- edit
- update
- destroy
Now edit PostController.php, which is the code below
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::latest()->paginate(5);
return view('post.index',compact('posts'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('post.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',
'detail' => 'required',
]);
Post::create($request->all());
return redirect()->route('posts.index')
->with('success','Post created successfully.');
}
/**
* Display the specified resource.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function show(Post $post)
{
return view('post.show',compact('post'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function edit(Post $post)
{
return view('post.edit',compact('post'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Post $post)
{
$request->validate([
'title' => 'required',
'detail' => 'required',
]);
$post->update($request->all());
return redirect()->route('posts.index')
->with('success','Post updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(Post $post)
{
$post->delete();
return redirect()->route('posts.index')
->with('success','Post deleted successfully');
}
}
Now go to app/Models/Post.php file and In this model edit following code
app/Models/Post.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'title', 'detail'
];
}
Step 6: Create View Files
Now go to the next step, in this step we should see the file for Laravel 8 project so go to the "resources/views/" folder and create a post folder then under post folder create the following blade file.
- layout.blade.php
- index.blade.php
- create.blade.php
- edit.blade.php
- show.blade.php
After create view blade file we following below code and copy this code one by one and edit your view file
resources/views/layout.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 8 CRUD Example For Beginners - phpicoder.com</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="jumbotron">
@yield('content')
</div>
</div>
</body>
</html>
resources/views/index.blade.php
@extends('post.layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="float-left">
<h2>Laravel 8 CRUD Example For Search - phpicoder.com</h2>
</div>
<div class="float-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">
{{ $message }}
</div>
@endif
<table class="bg-light table table-bordered ">
<tr>
<th>No</th>
<th>Name</th>
<th>Details</th>
<th width="280px">Action</th>
</tr>
@foreach ($posts as $post)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $post->title }}</td>
<td>{{ $post->detail }}</td>
<td>
<form action="{{ route('posts.destroy',$post->id) }}" method="POST">
<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>
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
{!! $posts->links() !!}
@endsection
resources/views/create.blade.php
@extends('post.layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="float-left">
<h2>Add New Post</h2>
</div>
<div class="float-right">
<a class="btn btn-primary" href="{{ route('posts.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('posts.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>Detail:</strong>
<textarea class="form-control" style="height:150px" name="detail" placeholder="Detail"></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/edit.blade.php
@extends('post.layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="float-left">
<h2>Edit Post</h2>
</div>
<div class="float-right">
<a class="btn btn-primary" href="{{ route('posts.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('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>Detail:</strong>
<textarea class="form-control" rows="4" name="detail" placeholder="Detail">{{ $post->detail }}</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/show.blade.php
@extends('post.layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="float-left">
<h2> Show Post</h2>
</div>
<div class="float-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>Title:</strong>
{{ $post->title }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Details:</strong>
{{ $post->detail }}
</div>
</div>
</div>
@endsection
Now laravel 8 crud application example is a ready so run bellow command for quick run:
After run above command go to browser and add http://localhost:8000/posts url Then You will see layout following
Now we finish laravel 8 CRUD Example and you need full code? if yes so go to github https://github.com/phpicoder/laravel-crud-example
I hope this tutorial help for you.
Related Post