0% found this document useful (0 votes)
0 views6 pages

Crud

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

Step 1: Set Up a Laravel Project

1. Install Laravel: Open your terminal and run the following command to create a new
Laravel project:
bash
Copy code
composer create-project laravel/laravel crud-app

This will create a new Laravel project named crud-app.

2. Navigate to your project directory:

cd crud-app

3. Start the Laravel Development Server:

php artisan serve

The application will be accessible at http://localhost:8000.

Step 2: Configure the Database


1. Create a Database: Create a database in your MySQL or preferred database management
system. For example, you can create a database named crud_app.

2. Update .env File: Open the .env file and update the database connection settings:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=crud_app
DB_USERNAME=root
DB_PASSWORD=

3. Run Migrations: Laravel comes with default migrations for the users table and other
necessary tables. Run:

php artisan migrate

Step 3: Create the Model, Controller, and Migration


1. Generate Model, Controller, and Migration for a Resource (e.g., Post): Use the
following Artisan command to generate the necessary files:

php artisan make:model Post -mcr

This will generate:


• Post model: app/Models/Post.php
• PostController: app/Http/Controllers/PostController.php
• A migration file for creating the posts table.
2. Define Schema in Migration: Open the generated migration file in
database/migrations/xxxx_xx_xx_create_posts_table.php and update
the schema:

public function up()


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

public function down()


{
Schema::dropIfExists('posts');
}

3. Run the Migration: Run the migration to create the posts table in the database:

php artisan migrate

Step 4: Set Up Routes


1. Define Resource Routes: In routes/web.php, define resource routes for the
PostController:

use App\Http\Controllers\PostController;

Route::resource('posts', PostController::class);

Step 5: Implement Controller Logic


1. Add CRUD Methods in PostController: Open the
app/Http/Controllers/PostController.php and implement the CRUD
operations.

<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller


{
// Display a listing of the resource
public function index()
{
$posts = Post::all();
return view('posts.index', compact('posts'));
}

// Show the form for creating a new resource


public function create()
{
return view('posts.create');
}

// Store a newly created resource in storage


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.');
}

// Display the specified resource


public function show(Post $post)
{
return view('posts.show', compact('post'));
}

// Show the form for editing the specified resource


public function edit(Post $post)
{
return view('posts.edit', compact('post'));
}

// Update the specified resource in storage


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.');
}

// Remove the specified resource from storage


public function destroy(Post $post)
{
$post->delete();
return redirect()->route('posts.index')->with('success', 'Post
deleted successfully.');
}
}
Step 6: Create Views
1. Create Views for CRUD Operations: Create the following views in the
resources/views/posts folder:

• index.blade.php (List all posts):

@extends('layouts.app')

@section('content')
<div class="container">
<h1>Posts</h1>
<a href="{{ route('posts.create') }}" class="btn btn-
primary">Create Post</a>
<table class="table mt-4">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach($posts as $post)
<tr>
<td>{{ $post->id }}</td>
<td>{{ $post->title }}</td>
<td>
<a href="{{ route('posts.show', $post->id) }}"
class="btn btn-info">View</a>
<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-block;">
@csrf
@method('DELETE')
<button class="btn
btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endsection

• create.blade.php (Create new post form):

@extends('layouts.app')

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

• edit.blade.php (Edit post form):

@extends('layouts.app')

@section('content')
<div class="container">
<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" class="form-control" id="title"
name="title" value="{{ $post->title }}" required>
</div>
<div class="form-group mt-3">
<label for="content">Content</label>
<textarea class="form-control" id="content"
name="content" required>{{ $post->content }}</textarea>
</div>
<button type="submit" class="btn btn-success
mt-3">Update</button>
</form>
</div>
@endsection

• show.blade.php (Display single post):

@extends('layouts.app')

@section('content')
<div class="container">
<h1>{{ $post->title }}</h1>
<p>{{ $post->content }}</p>
<a href="{{ route('posts.index') }}" class="btn btn-
secondary">Back to List</a>
</div>
@endsection

Step 7: Run the Application


1. Serve the Application: Run the Laravel development server if it's not already running:

php artisan serve


2. Test the CRUD Operations:
• Go to http://localhost:8000/posts to see the list of posts.
• Use the interface to create, read, update, and delete posts.

You might also like