create crud app in laravel
- User
create crud app in laravel
- Assistant
Creating a CRUD (Create, Read, Update, Delete) application in Laravel involves several steps. Below is a step-by-step guide to build a simple CRUD app for managing, for example, "Posts".
Step 1: Set up Laravel Project
If you haven't installed Laravel yet, first install Composer, then create a new Laravel project:
composer create-project laravel/laravel laravel-crud cd laravel-crud
Step 2: Set up Database
Configure your
.envfile with your database credentials:DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_crud DB_USERNAME=root DB_PASSWORD=your_passwordCreate the database
laravel_crudin your MySQL or preferred DBMS.
Step 3: Create Model and Migration
Create a model and migration for
Post:php artisan make:model Post -mThis creates:
app/Models/Post.phpdatabase/migrations/xxxx_xx_xx_create_posts_table.php
Edit the migration file to add fields:
public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->timestamps(); }); }Run the migration:
php artisan migrate
Step 4: Create Controller
Create a resource controller for Post:
php artisan make:controller PostController --resource
Step 5: Define Routes
In
routes/web.php, add:use App\Http\Controllers\PostController; Route::resource('posts', PostController::class);
Step 6: Implement Controller Methods
Edit
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.'); } }
Step 7: Create Views
Create a folder
resources/views/postsand add the following Blade templates:index.blade.php@extends('layout') @section('content') <h1>Posts</h1> <a href="{{ route('posts.create') }}">Create New Post</a> @if(session('success')) <p>{{ session('success') }}</p> @endif <ul> @foreach($posts as $post) <li> <a href="{{ route('posts.show', $post) }}">{{ $post->title }}</a> <a href="{{ route('posts.edit', $post) }}">Edit</a> <form action="{{ route('posts.destroy', $post) }}" method="POST" style="display:inline;"> @csrf @method('DELETE') <button type="submit">Delete</button> </form> </li> @endforeach </ul> @endsectioncreate.blade.php@extends('layout') @section('content') <h1>Create Post</h1> @if ($errors->any()) <div> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{ route('posts.store') }}" method="POST"> @csrf <label>Title:</label> <input type="text" name="title" value="{{ old('title') }}"> <br> <label>Content:</label> <textarea name="content">{{ old('content') }}</textarea> <br> <button type="submit">Create</button> </form> @endsectionedit.blade.php@extends('layout') @section('content') <h1>Edit Post</h1> @if ($errors->any()) <div> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{ route('posts.update', $post) }}" method="POST"> @csrf @method('PUT') <label>Title:</label> <input type="text" name="title" value="{{ old('title', $post->title) }}"> <br> <label>Content:</label> <textarea name="content">{{ old('content', $post->content) }}</textarea> <br> <button type="submit">Update</button> </form> @endsectionshow.blade.php@extends('layout') @section('content') <h1>{{ $post->title }}</h1> <p>{{ $post->content }}</p> <a href="{{ route('posts.index') }}">Back to Posts</a> @endsectionlayout.blade.phpCreate a simple layout file
resources/views/layout.blade.php:<!DOCTYPE html> <html> <head> <title>Laravel CRUD</title> </head> <body> @yield('content') </body> </html>
Step 8: Run the Application
Start the Laravel development server:
php artisan serveVisit
http://127.0.0.1:8000/postsin your browser to see the CRUD app in action.
Summary
You now have a basic Laravel CRUD app with:
- Database migration for posts
- Model and resource controller
- Routes for CRUD operations
- Blade views for listing, creating, editing, and showing posts
If you want, I can help you add features like pagination, authentication, or API support next!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.