Recent posts
Don't miss the latest trends
-
coders guild
imagenumbercontroller
public function post_update(Request $request, $id) { $request->validate([ 'title' => 'required|string|max:255', 'description' => 'required|string', 'post_number' => 'nullable|string|max:255', 'post_image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048', ]); $post = Post::findOrFail($id); if ($request->hasFile('post_image')) { $imagePath = $request->file('post_image')->store('post_images', 'public'); $post->post_image = $imagePath; } $post->update([ 'title' => $request->title, 'description' => $request->description, 'post_number' => $request->post_number, 'post_image' => $post->post_image, ]); return redirect()->back()->with('status', 'Post updated successfully!'); } public function post_store(Request $request) { $request->validate([ 'title' => 'required|string|max:255', 'description' => 'required|string', 'post_number' => 'nullable|string|max:255', 'post_image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048', ]); $imagePath = null; if ($request->hasFile('post_image')) { $imagePath = $request->file('post_image')->store('post_images', 'public'); } Post::create([ 'title' => $request->title, 'description' => $request->description, 'post_number' => $request->post_number, 'post_image' => $imagePath, ]); return redirect()->back()->with('status', 'Post added successfully!'); }
-
coders guild
image number
view: <td>{{ $post->post_number }}</td> <td> @if($post->post_image) <img src="{{ asset('storage/' . $post->post_image) }}" alt="Post Image" width="100"> @else No Image @endif </td> put in forms: <form action="{{ route('posts.update', $post->id) }}" method="POST" enctype="multipart/form-data"> edit modal: <div class="mb-3"> <label class="form-label">Post Number</label> <input type="text" class="form-control" name="post_number" value="{{ $post->post_number }}"> </div> <div class="mb-3"> <label class="form-label">Post Image</label> <input type="file" class="form-control" name="post_image"> @if($post->post_image) <img src="{{ asset('storage/' . $post->post_image) }}" alt="Post Image" width="100" class="mt-2"> @endif </div> add modal:<div class="mb-3"> <label class="form-label">Post Number</label> <input type="text" class="form-control" name="post_number"> </div> <div class="mb-3"> <label class="form-label">Post Image</label> <input type="file" class="form-control" name="post_image"> </div>
-
coders guild
postroute
Route::get('/post',[UserController::class,'post'])->name('posts.index'); Route::post('/post_store', [UserController::class, 'post_store'])->name('posts.store'); Route::put('/posts_update{id}', [UserController::class, 'post_update'])->name('posts.update'); Route::delete('/posts_destroy/{id}', [UserController::class, 'posts_destroy'])->name('posts.destroy');
-
coders guild
post controller
public function post() { $posts = Post::all(); return view('user.create', compact('posts')); } public function post_store(Request $request) { $request->validate([ 'title' => 'required|string|max:255', 'description' => 'required|string', ]); Post::create($request->only('title', 'description')); return redirect()->back()->with('status', 'Post added successfully!'); } public function post_update(Request $request, $id) { $request->validate([ 'title' => 'required|string|max:255', 'description' => 'required|string', ]); $post = Post::findOrFail($id); $post->update($request->only('title', 'description')); return redirect()->back()->with('status', 'Post updated successfully!'); } public function posts_destroy($id) { $post = Post::findOrFail($id); $post->delete(); return redirect()->back()->with('status', 'Post deleted successfully!'); } }
-
coders guild
post blade
@include('components.doctype') @include('components.header') <div class="container mt-4"> <h2>Posts</h2> <!-- Button trigger create modal --> <button type="button" class="btn btn-success mb-3" data-bs-toggle="modal" data-bs-target="#createModal"> Add Post </button> <!-- Posts Table --> <table class="table table-bordered table-striped"> <thead class="thead-dark"> <tr> <th>ID</th> <th>Title</th> <th>Description</th> <th>Actions</th> </tr> </thead> <tbody> @foreach ($posts as $post) <tr> <td>{{ $post->id }}</td> <td>{{ $post->title }}</td> <td>{{ $post->description }}</td> <td> <!-- Edit Button --> <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#editModal{{ $post->id }}"> Edit </button> <!-- Delete Form --> <form action="{{ route('posts.destroy', $post->id) }}" method="POST" style="display: inline-block;" onsubmit="return confirm('Are you sure you want to delete this post?');"> @csrf @method('DELETE') <button type="submit" class="btn btn-danger">Delete</button> </form> </td> </tr> <!-- Edit Modal --> <div class="modal fade" id="editModal{{ $post->id }}" tabindex="-1" aria-labelledby="editModalLabel{{ $post->id }}" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <form action="{{ route('posts.update', $post->id) }}" method="POST"> @csrf @method('PUT') <div class="modal-header"> <h5 class="modal-title" id="editModalLabel{{ $post->id }}">Edit Post</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> <div class="mb-3"> <label class="form-label">Title</label> <input type="text" class="form-control" name="title" value="{{ $post->title }}" required> </div> <div class="mb-3"> <label class="form-label">Description</label> <textarea class="form-control" name="description" rows="3" required>{{ $post->description }}</textarea> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="submit" class="btn btn-primary">Save Changes</button> </div> </form> </div> </div> </div> <!-- End Edit Modal --> @endforeach </tbody> </table> </div> <!-- Create Modal --> <div class="modal fade" id="createModal" tabindex="-1" aria-labelledby="createModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <form action="{{ route('posts.store') }}" method="POST"> @csrf <div class="modal-header"> <h5 class="modal-title" id="createModalLabel">Add New Post</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> <div class="mb-3"> <label class="form-label">Title</label> <input type="text" class="form-control" name="title" required> </div> <div class="mb-3"> <label class="form-label">Description</label> <textarea class="form-control" name="description" rows="3" required></textarea> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="submit" class="btn btn-success">Add Post</button> </div> </form> </div> </div> </div> @include('components.footer')
Popular Posts
-
Routes
1 reaction
-
installaion:
0 reaction
-
auth installation
0 reaction
-
profile route
0 reaction
-
profile
0 reaction
Jane Cooper
Katen Doe
Barbara Cartland