Laravel 11 CRUD
Creating a CRUD (Create, Read, Update, Delete) application in Laravel 11 involves several steps. Here’s a detailed guide to help you set up a basic CRUD system:
1. Set Up Laravel Project
If you haven`'t already set up a Laravel project, start by creating a new Laravel project:
composer create-project --prefer-dist laravel/laravel laravel-crud
cd laravel-crud 2. Create a Migration
For example, if you’re creating a bb table:
php artisan make:migration create_posts_table --create=postsEdit the migration file located in database/migrations/xxxx_xx_xx_create_posts_table.php to define the schema:
// Example migration schema definition
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 migrate3. Create a Model
Generate a model for your table:
php artisan make:model PostThe model file will be located at app/Models/Post.php. Ensure it looks like this:
namespace App\
use Illuminate\
Database\
Eloquent\
Factories\
HasFactory;
use Illuminate\
Database\
Eloquent\
Model;
class Postextends Model {
use HasFactory;
protected $fillable = [
'title',
'content'
];
}4. Create a Controller
Generate a controller to handle CRUD operations:
php artisan make:controller PostControllerEdit the controller file located at app/Http/Controllers/PostController.php:
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.');
}
}5. Define Routes
Add routes for your CRUD operations in routes/web.php:
use App\Http\Controllers\PostController;
Route::resource('posts', PostController::class);6. Create Views
Create Blade templates for your views. Here’s a basic setup:
- Index View (
resources/views/posts/index.blade.php):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Posts</title>
</head>
<body>
<h1>Posts</h1>
<a href="{{ route('posts.create') }}">Create New Post</a>
<table>
<thead>
<tr>
<th>Title</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach($posts as $post)
<tr>
<td>{{ $post->title }}</td>
<td>
<a href="{{ route('posts.show', $post->id) }}">View</a>
<a href="{{ route('posts.edit', $post->id) }}">Edit</a>
<form action="{{ route('posts.destroy', $post->id) }}" method="POST" style={{ display: 'inline' }}>
@csrf
@method('DELETE')
<button type="submit">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>- Create View (
resources/views/posts/create.blade.php):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create Post</title>
</head>
<body>
<h1>Create Post</h1>
<form action="{{ route('posts.store') }}" method="POST">
@csrf
<label htmlFor="title">Title:</label>
<input type="text" name="title" id="title" required />
<label htmlFor="content">Content:</label>
<textarea name="content" id="content" required></textarea>
<button type="submit">Save</button>
</form>
</body>
</html>- Edit View (
resources/views/posts/edit.blade.php):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Post</title>
</head>
<body>
<h1>Edit Post</h1>
<form action="{{ route('posts.update', $post->id) }}" method="POST">
@csrf
@method('PUT')
<label htmlFor="title">Title:</label>
<input type="text" name="title" id="title" value="{{ $post->title }}" required />
<label htmlFor="content">Content:</label>
<textarea name="content" id="content" required>{{ $post->content }}</textarea>
<button type="submit">Update</button>
</form>
</body>
</html>- Show View (
resources/views/posts/show.blade.php):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Show Post</title>
</head>
<body>
<h1>{{ $post->title }}</h1>
<div>
{!! $post->content !!}
</div>
<a href="{{ route('posts.edit', $post->id) }}">Edit</a>
<form action="{{ route('posts.destroy', $post->id) }}" method="POST">
@csrf
@method('DELETE')
<button type="submit">Delete</button>
</form>
<a href="{{ route('posts.index') }}">Back to Posts</a>
</body>
</html>Summary
- Migration: Define the database schema.
- Model: Define the Eloquent model.
- Controller: Handle CRUD operations.
- Routes: Define routes for resource controller.
- Views: Create Blade templates for creating, editing, viewing, and listing records.