Simple laravel example with CRUD operations
Here is a simple example of creating a basic CRUD (Create, Read, Update, Delete) application using Laravel, a popular PHP web application framework.
Step-by-Step Guide to Creating a CRUD Application in Laravel
Step 1: Install Laravel
Make sure you have Composer installed. Then, install a new Laravel project:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Set Up Database Configuration
Open the .env
file in the root directory of your Laravel project and set up your database connection details:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=
Step 3: Create a Migration
Generate a migration for creating a posts
table:
php artisan make:migration create_posts_table --create=posts
Edit the generated migration file in database/migrations/xxxx_xx_xx_xxxxxx_create_posts_table.php
:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});
}
Run the migration to create the table:
php artisan migrate
Step 4: Create a Model and Controller
Generate a model and a resource controller for Post
:
php artisan make:model Post -mcr
This command will create:
- A
Post
model (app/Models/Post.php
) - A migration file for
posts
table (database/migrations/xxxx_xx_xx_xxxxxx_create_posts_table.php
) - A resource controller (
app/Http/Controllers/PostController.php
)
Step 5: Define Routes
Open the routes/web.php
file and define routes for the Post
resource:
use App\Http\Controllers\PostController;
Route::resource('posts', PostController::class);
Step 6: Implement Controller Methods
Open the PostController.php
and implement the methods:
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index()
{
$posts = Post::all();
return view('posts.index', compact('posts'));
}
public function create()
{
return view('posts.create');
}
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'content' => 'required',
]);
Post::create($request->all());
return redirect()->route('posts.index')
->with('success', 'Post created successfully.');
}
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
public function edit(Post $post)
{
return view('posts.edit', compact('post'));
}
public function update(Request $request, Post $post)
{
$request->validate([
'title' => 'required',
'content' => 'required',
]);
$post->update($request->all());
return redirect()->route('posts.index')
->with('success', 'Post updated successfully.');
}
public function destroy(Post $post)
{
$post->delete();
return redirect()->route('posts.index')
->with('success', 'Post deleted successfully.');
}
}
Step 7: Create Views
Create the following Blade template files for the views:
-
resources/views/posts/index.blade.php
<!DOCTYPE html> <html> <head> <title>Laravel 8 CRUD Application</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <body> <div class="container"> <h2>Laravel 8 CRUD Application</h2> <a class="btn btn-success" href="{{ route('posts.create') }}">Create New Post</a> @if ($message = Session::get('success')) <div class="alert alert-success"> <p>{{ $message }}</p> </div> @endif <table class="table table-bordered"> <tr> <th>No</th> <th>Title</th> <th>Content</th> <th width="280px">Action</th> </tr> @foreach ($posts as $post) <tr> <td>{{ ++$i }}</td> <td>{{ $post->title }}</td> <td>{{ $post->content }}</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() !!} </div> </body> </html>
-
resources/views/posts/create.blade.php
<!DOCTYPE html> <html> <head> <title>Laravel 8 CRUD Application</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <body> <div class="container"> <h2>Create New Post</h2> @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="form-group"> <strong>Title:</strong> <input type="text" name="title" class="form-control" placeholder="Title"> </div> <div class="form-group"> <strong>Content:</strong> <textarea class="form-control" style="height:150px" name="content" placeholder="Content"></textarea> </div> <div class="form-group"> <button type="submit" class="btn btn-primary">Submit</button> </div> </form> </div> </body> </html>
-
resources/views/posts/edit.blade.php
<!DOCTYPE html> <html> <head> <title>Laravel 8 CRUD Application</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <body> <div class="container"> <h2>Edit Post</h2> @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="form-group"> <strong>Title:</strong> <input type="text" name="title" value="{{ $post->title }}" class="form-control" placeholder="Title"> </div> <div class="form-group"> <strong>Content:</strong> <textarea class="form-control" style="height:150px" name="content" placeholder="Content">{{ $post->content }}</textarea> </div> <div class="form-group"> <button type="submit" class="btn btn-primary">Submit</button> </div> </form> </div> </body> </html>
-
resources/views/posts/show.blade.php
<!DOCTYPE html> <html> <head> <title>Laravel 8 CRUD Application</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <body> <div class="container"> <h2>Show Post</h2> <div class="form-group"> <strong>Title:</strong> {{ $post->title }} </div> <div class="form-group"> <strong>Content:</strong> {{ $post->content }} </div> <div class="form-group"> <a class="btn btn-primary" href="{{ route('posts.index') }}"> Back</a> </div> </div> </body> </html>
Step 8: Run the Application
Finally, start the Laravel development