Crud
Crud
Crud
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
cd 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:
3. Run the Migration: Run the migration to create the posts table in the database:
use App\Http\Controllers\PostController;
Route::resource('posts', PostController::class);
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
Post::create($request->all());
return redirect()->route('posts.index')->with('success', 'Post
created successfully.');
}
$post->update($request->all());
return redirect()->route('posts.index')->with('success', 'Post
updated successfully.');
}
@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
@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
@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
@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