This repository has been archived on 2025-07-15. You can view files and clone it, but cannot push or open issues or pull requests.
Files
lean_in_world/app/Http/Controllers/Admin/ManageBlogController.php
meghamalore eed0ce11d2 first commit
2024-07-04 16:57:26 +05:30

159 lines
4.7 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Admin\blog_category;
use App\Models\Admin\blog;
use Illuminate\Http\Request;
use Carbon\Carbon;
class ManageBlogController extends Controller
{
public function index()
{
$blog_category = Blog::orderBy('created_at', 'desc')->get()->toArray();
// dd($blog_category);
return view('Admin.Pages.manage_cms.manage_blogs.manage_blog')->with(['blog'=>$blog_category]);
}
public function add_blog()
{
$blog = blog_category::all()->toArray();
return view('Admin.Pages.manage_cms.manage_blogs.add_blog')->with(['blog_category'=>$blog]);
}
public function insert_blog(Request $request)
{
// for image upload start
$file = $request->file('file');
$ext = $file->extension();
$file_name = time().'.'.$ext;
$path = public_path().'/assets/uploads/blogs';
$file-> move($path,$file_name);
// for image upload end
$blog = new blog;
$blog->title = $request->input('blog_title');
$blog->blog_categories_xid = $request->input('blog_category');
$blog->description = $request->input('blog_discription');
$blog->blog_image = $file_name;
$blog->publish_date = $request->input('publish_date');
$blog->save();
return response()->json(['success' => true,'status'=>200]);
}
public function view_blog($id)
{
$blog_categ = blog_category::all()->toArray();
$blog = blog::find($id);
return view('Admin.Pages.manage_cms.manage_blogs.view_blog')->with(['single_blog_detail'=>$blog,'blog_category'=>$blog_categ]);;
}
public function edit_blog($id)
{
$blog_categ = blog_category::all()->toArray();
$blog = blog::find($id);
return view('Admin.Pages.manage_cms.manage_blogs.edit_blog')->with(['single_blog_detail'=>$blog,'blog_category'=>$blog_categ]);
}
public function update_blog(Request $request)
{
$blog = Blog::find($request->edit_blog_id);
// Check if any field values are different from the form data
$isChanged = false;
// Compare title
if ($blog->title != $request->input('title')) {
$blog->title = $request->input('title');
$isChanged = true;
}
// Compare blog category
if ($blog->blog_categories_xid != $request->input('blog_category')) {
$blog->blog_categories_xid = $request->input('blog_category');
$isChanged = true;
}
// Compare description
if ($blog->description != $request->input('description')) {
$blog->description = $request->input('description');
$isChanged = true;
}
// Compare publish date
if ($blog->publish_date != $request->input('publish_date')) {
$blog->publish_date = $request->input('publish_date');
$isChanged = true;
}
// Handle file upload if a file is provided
if ($request->hasFile('file') && $request->file('file')->isValid()) {
$file_one = $request->file('file');
$ext = $file_one->extension();
$file_name_main_image = time() . '.' . $ext;
$path = public_path() . '/assets/uploads/blogs';
$file_one->move($path, $file_name_main_image);
// Compare blog image
if ($blog->blog_image != $file_name_main_image) {
$blog->blog_image = $file_name_main_image;
$isChanged = true;
}
}
// Save the blog if there are changes
if ($isChanged) {
$blog->save();
}
return response()->json(['success' => true, 'status' => 200]);
}
// blog blog start here
public function index_categories()
{
$blog_category = blog_category::all()->toArray();
return view('Admin.Pages.manage_cms.manage_blogs.manage_blog_categ.manage_blog_categories')->with(['category'=>$blog_category]);
}
public function insert_category(Request $request)
{
$blog = new blog_category;
$blog->category_name = $request->input('category_name');
$blog->save();
return response()->json(['success' => true,'status'=>200]);
}
public function update_category(Request $request)
{
$blog = blog_category::find($request->edit_category_id);
$blog->category_name = $request->input('edit_category_name');
$blog->save();
return response()->json(['success' => true,'status'=>200]);
}
public function delete_blog_categ($id)
{
$blog_categ = blog_category::find($id)->delete();
}
public function delete_blog($id)
{
$blog_categ = blog::find($id)->delete();
}
// blog blog end here
// blog blog start here
}