Skip to content

Commit 67e2355

Browse files
committed
Laravel Request module.
1 parent ccdc79f commit 67e2355

File tree

9 files changed

+418
-6
lines changed

9 files changed

+418
-6
lines changed

app/Http/Controllers/PostController.php

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,102 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Http\Requests\StorePostRequest;
6+
use App\Models\Post;
7+
use Illuminate\Http\RedirectResponse;
58
use Illuminate\Http\Request;
9+
use Illuminate\View\View;
610

711
class PostController extends Controller
812
{
9-
//
13+
public function index(): View
14+
{
15+
$posts = Post::latest()->get();
16+
return view('admin.blog.index', compact('posts'));
17+
}
18+
19+
public function create(): View
20+
{
21+
return view('admin.blog.create');
22+
}
23+
24+
// public function store(Request $request): RedirectResponse
25+
// {
26+
// $request->validate([
27+
// // 'title' => 'bail|required|max:191',
28+
// 'title' => 'required',
29+
// 'description' => 'required',
30+
// 'images' => 'required',
31+
// 'status' => 'required'
32+
// ]);
33+
34+
// $input = $request->all();
35+
36+
// if ($postimg = $request->file('images')) {
37+
// $imagepath = public_path('storage/post');
38+
// $postimgname = date('YmdHis') . '.' . $postimg->getClientOriginalExtension();
39+
// $postimg->move($imagepath, $postimgname);
40+
// $input['images'] = 'storage/post/' . $postimgname;
41+
// }
42+
43+
// // if ($input['status'] == 'Active')
44+
// // {
45+
// // Post::create([
46+
// // 'title' => $input['title'],
47+
// // 'description' => $input['description'],
48+
// // 'images' => $input['images'],
49+
// // 'status' => $input['status']
50+
// // ]);
51+
// // }
52+
// Post::create([
53+
// 'title' => $input['title'],
54+
// 'description' => $input['description'],
55+
// 'images' => $input['images'],
56+
// 'status' => $input['status']
57+
// ]);
58+
// // dd($input);
59+
60+
// return redirect()->route('blog.home')->with('success', 'Post Created Successfully!');
61+
// }
62+
63+
// Using Form Request class:
64+
public function store(StorePostRequest $request): RedirectResponse
65+
{
66+
$validated = $request->validated();
67+
68+
$validated = $request->safe()->except(['title','title']);
69+
$validated = $request->safe()->except(['description','description']);
70+
$validated = $request->safe()->except(['images','images']);
71+
$validated = $request->safe()->except(['status','status']);
72+
73+
$input = $request->all();
74+
// dd($postimg = $request->file('images'));
75+
76+
if($postimg = $request->file('images'))
77+
{
78+
$imagepath = public_path('storage/post');
79+
$postname = date('YmdHis').'.'.$postimg->getClientOriginalExtension();
80+
$postimg->move($imagepath, $postname);
81+
$input['images'] = 'storage/post/'.$postname;
82+
}
83+
84+
// dd($input['images']);
85+
86+
Post::create([
87+
'title' => $input['title'],
88+
'description' => $input['description'],
89+
'images' => $input['images'],
90+
'status' => $input['status']
91+
]);
92+
93+
// dd($input);
94+
95+
return redirect()->route('blog.home')->with('success', 'Post Created Successfully!');
96+
}
97+
98+
public function edit($id)
99+
{
100+
$posts = Post::find($id);
101+
return view('admin.blog.edit', compact('posts'));
102+
}
10103
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class StorePostRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*/
12+
public function authorize(): bool
13+
{
14+
return true;
15+
}
16+
17+
/**
18+
* Get the validation rules that apply to the request.
19+
*
20+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
21+
*/
22+
public function rules(): array
23+
{
24+
return [
25+
// Custome rule here:
26+
27+
'title' => 'required',
28+
'description' => 'required',
29+
'images' => 'required',
30+
'status' => 'required'
31+
];
32+
}
33+
34+
public function messages()
35+
{
36+
return [
37+
'title.required' => 'Title is required!',
38+
'description.required' => 'Description is required',
39+
'images.required' => 'Please select an image.',
40+
'status.required' => 'Status is required'
41+
];
42+
}
43+
}

app/Models/Post.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\SoftDeletes;
8+
9+
class Post extends Model
10+
{
11+
use HasFactory, SoftDeletes;
12+
13+
protected $table = 'posts';
14+
15+
protected $fillable = ['title', 'description', 'images', 'status'];
16+
17+
protected $dates = ['deleted_at'];
18+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('posts', function (Blueprint $table) {
15+
$table->id();
16+
$table->string('title');
17+
$table->string('images')->nullable();
18+
$table->text('description');
19+
$table->enum('status', array('Active', 'Inactive', 'Draft'))->default('Draft')->nullable();
20+
$table->timestamps();
21+
$table->softDeletes();
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*/
28+
public function down(): void
29+
{
30+
Schema::dropIfExists('posts');
31+
}
32+
};
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
@extends('layouts.admin')
2+
@section('content')
3+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
4+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
5+
<div class="row">
6+
<div class="col-lg-12 margin-tb">
7+
<div class="pull-left">
8+
<h2>Add New Post</h2>
9+
</div>
10+
<div class="pull-right">
11+
<a class="btn btn-primary m-2" href="{{ route('blog.home') }}"> Back</a>
12+
</div>
13+
</div>
14+
</div>
15+
16+
@if ($errors->any())
17+
<div class="alert alert-danger">
18+
<ul>
19+
@foreach ($errors->all() as $error)
20+
<li>{{ $error }}</li>
21+
@endforeach
22+
</ul>
23+
</div>
24+
@endif
25+
26+
<form action="{{route('blog.store')}}" method="post" enctype="multipart/form-data">
27+
28+
@csrf
29+
30+
<div class="row">
31+
<div class="col-md-6">
32+
<div class="form-group">
33+
<strong>Title</strong>
34+
<input type="text" name="title" class="form-control" placeholder="Name">
35+
</div>
36+
</div>
37+
38+
<div class="col-md-6">
39+
<div class="form-group">
40+
<strong class="mb-2">Upload Image:</strong>
41+
<input type="file" class="form-control" id="images" name="images">
42+
</div>
43+
</div>
44+
45+
<div class="col-md-12">
46+
<div class="form-group">
47+
<strong>Description:</strong>
48+
<textarea class="form-control" style="height: 150px" name="description" placeholder="Description"></textarea>
49+
</div>
50+
</div>
51+
52+
<div class="col-md-12">
53+
<div class="form-check">
54+
<input class="form-check-input" type="radio" name="status" id="status" value="Active">
55+
<label class="form-check-label" for="status">Active</label>
56+
</div>
57+
<div class="form-check">
58+
<input class="form-check-input" type="radio" name="status" id="status" value="Inactive">
59+
<label class="form-check-label" for="status">Inactive</label>
60+
</div>
61+
<div class="form-check">
62+
<input class="form-check-input" type="radio" name="status" id="status" value="Draft" checked>
63+
<label class="form-check-label" for="status">Draft</label>
64+
</div>
65+
<button type="submit" class="btn btn-primary float-end">Create</button>
66+
</div>
67+
</div>
68+
</form>
69+
@endsection
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
@extends('layouts.admin')
2+
@section('content')
3+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
4+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
5+
<div class="row">
6+
<div class="col-lg-12 margin-tb">
7+
<div class="pull-left">
8+
<h2>Add New Post</h2>
9+
</div>
10+
<div class="pull-right">
11+
<a class="btn btn-primary m-2" href="{{ route('blog.home') }}"> Back</a>
12+
</div>
13+
</div>
14+
</div>
15+
16+
@if ($errors->any())
17+
<div class="alert alert-danger">
18+
<ul>
19+
@foreach ($errors->all() as $error)
20+
<li>{{ $error }}</li>
21+
@endforeach
22+
</ul>
23+
</div>
24+
@endif
25+
26+
<form action="{{route('blog.update', $posts->id)}}" method="post" enctype="multipart/form-data">
27+
28+
@csrf
29+
30+
<div class="row">
31+
<div class="col-md-6">
32+
<div class="form-group">
33+
<strong>Title</strong>
34+
<input type="text" name="title" class="form-control" placeholder="Name" value="{{$posts->title}}">
35+
</div>
36+
</div>
37+
38+
<div class="col-md-6">
39+
<div class="form-group">
40+
<strong class="mb-2">Upload Image:</strong>
41+
<input type="file" class="form-control" id="images" name="images">
42+
</div>
43+
</div>
44+
45+
<div class="col-md-12">
46+
<div class="form-group">
47+
<strong>Description:</strong>
48+
<textarea class="form-control" style="height: 150px" name="description" placeholder="Description">{{$posts->description}}</textarea>
49+
</div>
50+
</div>
51+
52+
<div class="col-md-12">
53+
<div class="form-check">
54+
<input class="form-check-input" type="radio" name="status" id="status" value="Active" {{ $posts->status == 'Active' ? 'checked' : '' }}>
55+
<label class="form-check-label" for="status">Active</label>
56+
</div>
57+
<div class="form-check">
58+
<input class="form-check-input" type="radio" name="status" id="status" value="Inactive" {{ $posts->status == 'Inactive' ? 'checked' : '' }}>
59+
<label class="form-check-label" for="status">Inactive</label>
60+
</div>
61+
<div class="form-check">
62+
<input class="form-check-input" type="radio" name="status" id="status" value="Draft" {{ $posts->status == 'Draft' ? 'checked' : '' }}>
63+
<label class="form-check-label" for="status">Draft</label>
64+
</div>
65+
<button type="submit" class="btn btn-primary float-end">Update</button>
66+
</div>
67+
68+
<div class="col-md-12">
69+
<img src="/{{$posts->images}}" alt="{{$posts->title}}" width="150px">
70+
</div>
71+
</div>
72+
</form>
73+
@endsection
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
@extends('layouts.admin')
2+
@section('content')
3+
<div class="row">
4+
<div class="col-lg-12 margin-tb">
5+
<div class="pull-left">
6+
<h2>Blog Post Managment</h2>
7+
</div>
8+
<div class="pull-right">
9+
<a class="btn btn-success m-2" href="{{ route('blog.create') }}"> Create New Product</a>
10+
</div>
11+
</div>
12+
</div>
13+
14+
@if ($message = Session::get('success'))
15+
<div class="alert alert-success">
16+
<p>{{ $message }}</p>
17+
</div>
18+
@endif
19+
20+
<table class="table table-bordered">
21+
<tr>
22+
<th>#</th>
23+
<th>Title</th>
24+
<th>Description</th>
25+
<th>Images</th>
26+
<th>Status</th>
27+
<th width="280px">Action</th>
28+
</tr>
29+
@foreach ($posts as $i => $post)
30+
<tr>
31+
<td>{{ ++$i }}</td>
32+
<td class="text-center">{{ $post->title }}</td>
33+
<td>{{ Str::limit($post->description, 10) }}</td>
34+
<td><img src="{{$post->images}}" alt="{{$post->title}}" width="90px" height="50px"></td>
35+
<td class="text-center">{{$post->status}}</td>
36+
<td>
37+
<form action="{{ route('blog.delete',$post->id) }}" method="POST">
38+
<a class="btn btn-info" href="{{ route('blog.show',$post->id) }}">Show</a>
39+
@can('product-edit')
40+
<a class="btn btn-primary" href="{{ route('blog.edit',$post->id) }}">Edit</a>
41+
@endcan
42+
@csrf
43+
@method('DELETE')
44+
@can('product-delete')
45+
<button type="submit" class="btn btn-danger">Delete</button>
46+
@endcan
47+
</form>
48+
</td>
49+
</tr>
50+
@endforeach
51+
</table>
52+
@endsection

0 commit comments

Comments
 (0)