Using the Laravel CRUD Example tutorial you will learn how to create CRUD operation in Laravel Application. Creating a CRUD (Create, Read, Update, Delete) application in Laravel involves creating a database schema, defining routes, controllers, and views to handle database operations. Here’s a general outline of the steps involved in creating a CRUD application in Laravel step by step for beginners.
Step 1: Install Laravel App
Make sure you have Laravel installed on your machine. If not, you can install it using Composer by running the following command
composer create-project --prefer-dist laravel/laravel laravelblog
Step 2: Configure MySQL Database
Open the .env file in the root directory of your Laravel project and update the database connection details, including the database name, username, password, and host.
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: Generate the model and migration
In Laravel, a model represents a database table, and a migration defines the structure of the table. Run the following command to generate a model and migration for your CRUD entity.
php artisan make:model Post -m
This will generate a model file and a migration file in the app/Models and database/migrations directories, respectively.
Open the migration file created database/migrations/*.php and define the columns you want in your CRUD entity.
<?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')->nullable();
$table->longText('description')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
Execute the migration command to create the table in the database.
php artisan migrate
After running migration command you will find “app/Models/Post.php” and put the below same code.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'title', 'description'
];
}
Step 5: Define routes for CRUD operations
Open the routes/web.php file and define the routes for CRUD operations. You can use Laravel’s resource routing feature to generate the routes automatically.
Route::resource('posts', 'PostController');
Step 6: Generate CRUD controller
Run the following command to generate a controller for your CRUD Operation.
php artisan make:controller PostController --resource
This will generate a controller file in the app/Http/Controllers directory with predefined CRUD methods.
Open the controller file app/Http/Controllers/PostController.php and implement the CRUD methods.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
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 int $id
* @return \Illuminate\Http\Response
*/
public function show(Post $post)
{
return view('posts.show',compact('post'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @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 int $id
* @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 int $id
* @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
In this step, you need to create two folders “layouts” and “posts” inside the resources/views directory. Now navigate the resources/views/layouts directory and create an app.blade.php file on it.
Now open the resources/views/layouts/app.blade.php file and update the following code on it.
<!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>
Next, navigate the resources/views/posts directory and create a file index.blade.php file. After that open the resources/views/posts/index.blade.php file and put the below code on it.
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 6 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>
<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>
@endsection
Same as above, navigate the resources/views/posts directory and create a file create.blade.php file. After that open the resources/views/posts/create.blade.php file and put the below code on it.
@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
Same as above, navigate the resources/views/posts directory and create a file edit.blade.php file. After that open the resources/views/posts/edit.blade.php file and put the below code on it.
@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
Same as above, navigate the resources/views/posts directory and create a file show.blade.php file. After that open the resources/views/posts/show.blade.php file and put the below code on it.
@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 CRUD Example Tutorial For Beginners From Scratch is over. In this CRUD app.
php artisan serve
Now you can open the following URL on your browser: http://localhost:8000/posts and enjoy the Laravel CRUD operation.
Hope this tutorial help you to learning laravel 7 crud tutorial. If you have any question regarding laravel crud operation generator then please comment below we appreciate your comment.
Laravel 7 Crud Example Tutorial Bootstrap 4 Mysql Database
eraddffed
layout.blade.php code is missing here
I am getting following error while followed your steps on Ubuntu.
Illuminate\Contracts\Container\BindingResolutionException
Target class [app\Http\Controllers\PostController] does not exist.
Please add this ” use App\Http\Controllers\PostController; ” top after “use Illuminate\Support\Facades\Route;” this