Laravel 8 CRUD Tutorial Example Step By Step From Scratch

Laravel 8 CRUD Tutorial Example Step By Step From Scratch

Laravel CRUD Application Tutorial

This step-by-step guide will help you build a simple CRUD (Create, Read, Update, Delete) application in Laravel for managing blog posts.

Step 1: Install Laravel

First, install Laravel using Composer:

composer create-project --prefer-dist laravel/laravel laravel-crud

Once installed, navigate into your project directory:

cd laravel-crud

Step 2: Configure the Database

  1. Create a new database using MySQL (or your preferred DBMS).

  2. Open the .env file and update your database credentials:

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_database_name DB_USERNAME=your_database_user DB_PASSWORD=your_database_password
  1. Run the following command to apply Laravel's default migrations:

php artisan migrate

Step 3: Create a Model and Migration

We’ll create a Post model and a migration for the posts table:

php artisan make:model Post -m

Edit the generated migration file at database/migrations/xxxx_xx_xx_create_posts_table.php:

public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('body'); $table->timestamps(); }); }

Now run the migration:

php artisan migrate

Step 4: Create a Controller

Generate a controller for the CRUD operations:

php artisan make:controller PostController

Then open app/Http/Controllers/PostController.php and add the following logic:

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|string|max:255', 'body' => 'required|string', ]); Post::create($request->all()); return redirect()->route('posts.index')->with('success', 'Post created successfully.'); } public function edit(Post $post) { return view('posts.edit', compact('post')); } public function update(Request $request, Post $post) { $request->validate([ 'title' => 'required|string|max:255', 'body' => 'required|string', ]); $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 5: Define Routes

Add a resource route for the PostController in routes/web.php:

use App\Http\Controllers\PostController; Route::resource('posts', PostController::class);

Step 6: Create Blade Views

1. Index Viewresources/views/posts/index.blade.php

@extends('layouts.app') @section('content') <h1>All Posts</h1> <a href="{{ route('posts.create') }}" class="btn btn-primary">Create Post</a> @if(session('success')) <div class="alert alert-success">{{ session('success') }}</div> @endif <table class="table"> <thead> <tr> <th>Title</th> <th>Body</th> <th>Actions</th> </tr> </thead> <tbody> @foreach($posts as $post) <tr> <td>{{ $post->title }}</td> <td>{{ $post->body }}</td> <td> <a href="{{ route('posts.edit', $post->id) }}" class="btn btn-warning">Edit</a> <form action="{{ route('posts.destroy', $post->id) }}" method="POST" style="display:inline;"> @csrf @method('DELETE') <button type="submit" class="btn btn-danger">Delete</button> </form> </td> </tr> @endforeach </tbody> </table> @endsection

2. Create Viewresources/views/posts/create.blade.php

@extends('layouts.app') @section('content') <h1>Create Post</h1> <form action="{{ route('posts.store') }}" method="POST"> @csrf <div class="form-group"> <label for="title">Title</label> <input type="text" name="title" id="title" class="form-control" required> </div> <div class="form-group"> <label for="body">Body</label> <textarea name="body" id="body" class="form-control" required></textarea> </div> <button type="submit" class="btn btn-success">Save</button> </form> @endsection

3. Edit Viewresources/views/posts/edit.blade.php

@extends('layouts.app') @section('content') <h1>Edit Post</h1> <form action="{{ route('posts.update', $post->id) }}" method="POST"> @csrf @method('PUT') <div class="form-group"> <label for="title">Title</label> <input type="text" name="title" id="title" value="{{ $post->title }}" class="form-control" required> </div> <div class="form-group"> <label for="body">Body</label> <textarea name="body" id="body" class="form-control" required>{{ $post->body }}</textarea> </div> <button type="submit" class="btn btn-primary">Update</button> </form> @endsection

Step 7: Run the Application

Start the development server:

php artisan serve

Visit http://localhost:8000/posts in your browser.

 Features

  • Create a post

  • View a list of all posts

  • Edit and update a post

  • Delete a post

Next Steps

You can enhance this CRUD app by:

  • Adding authentication using php artisan make:auth or Laravel Breeze/Fortify

  • Implementing categories or tags

  • Adding image uploads

  • Integrating AJAX or Vue.js/React for dynamic updates

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close