BlogController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Models\BlogCategory;
  6. use App\Models\Blog;
  7. class BlogController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return \Illuminate\Http\Response
  13. */
  14. public function index(Request $request)
  15. {
  16. $sort_search = null;
  17. $blogs = Blog::orderBy('created_at', 'desc');
  18. if ($request->search != null){
  19. $blogs = $blogs->where('title', 'like', '%'.$request->search.'%');
  20. $sort_search = $request->search;
  21. }
  22. $blogs = $blogs->paginate(15);
  23. return view('backend.blog_system.blog.index', compact('blogs','sort_search'));
  24. }
  25. /**
  26. * Show the form for creating a new resource.
  27. *
  28. * @return \Illuminate\Http\Response
  29. */
  30. public function create()
  31. {
  32. $blog_categories = BlogCategory::all();
  33. return view('backend.blog_system.blog.create', compact('blog_categories'));
  34. }
  35. /**
  36. * Store a newly created resource in storage.
  37. *
  38. * @param \Illuminate\Http\Request $request
  39. * @return \Illuminate\Http\Response
  40. */
  41. public function store(Request $request)
  42. {
  43. $request->validate([
  44. 'category_id' => 'required',
  45. 'title' => 'required|max:255',
  46. ]);
  47. $blog = new Blog;
  48. $blog->category_id = $request->category_id;
  49. $blog->title = $request->title;
  50. $blog->banner = $request->banner;
  51. $blog->slug = preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $request->slug));
  52. $blog->short_description = $request->short_description;
  53. $blog->description = $request->description;
  54. $blog->meta_title = $request->meta_title;
  55. $blog->meta_img = $request->meta_img;
  56. $blog->meta_description = $request->meta_description;
  57. $blog->meta_keywords = $request->meta_keywords;
  58. $blog->save();
  59. flash(translate('Blog post has been created successfully'))->success();
  60. return redirect()->route('blog.index');
  61. }
  62. /**
  63. * Display the specified resource.
  64. *
  65. * @param int $id
  66. * @return \Illuminate\Http\Response
  67. */
  68. public function show($id)
  69. {
  70. }
  71. /**
  72. * Show the form for editing the specified resource.
  73. *
  74. * @param int $id
  75. * @return \Illuminate\Http\Response
  76. */
  77. public function edit($id)
  78. {
  79. $blog = Blog::find($id);
  80. $blog_categories = BlogCategory::all();
  81. return view('backend.blog_system.blog.edit', compact('blog','blog_categories'));
  82. }
  83. /**
  84. * Update the specified resource in storage.
  85. *
  86. * @param \Illuminate\Http\Request $request
  87. * @param int $id
  88. * @return \Illuminate\Http\Response
  89. */
  90. public function update(Request $request, $id)
  91. {
  92. $request->validate([
  93. 'category_id' => 'required',
  94. 'title' => 'required|max:255',
  95. ]);
  96. $blog = Blog::find($id);
  97. $blog->category_id = $request->category_id;
  98. $blog->title = $request->title;
  99. $blog->banner = $request->banner;
  100. $blog->slug = preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $request->slug));
  101. $blog->short_description = $request->short_description;
  102. $blog->description = $request->description;
  103. $blog->meta_title = $request->meta_title;
  104. $blog->meta_img = $request->meta_img;
  105. $blog->meta_description = $request->meta_description;
  106. $blog->meta_keywords = $request->meta_keywords;
  107. $blog->save();
  108. flash(translate('Blog post has been updated successfully'))->success();
  109. return redirect()->route('blog.index');
  110. }
  111. public function change_status(Request $request) {
  112. $blog = Blog::find($request->id);
  113. $blog->status = $request->status;
  114. $blog->save();
  115. return 1;
  116. }
  117. /**
  118. * Remove the specified resource from storage.
  119. *
  120. * @param int $id
  121. * @return \Illuminate\Http\Response
  122. */
  123. public function destroy($id)
  124. {
  125. Blog::find($id)->delete();
  126. return redirect('admin/blogs');
  127. }
  128. public function all_blog() {
  129. $blogs = Blog::where('status', 1)->orderBy('created_at', 'desc')->paginate(12);
  130. return view("frontend.blog.listing", compact('blogs'));
  131. }
  132. public function blog_details($slug) {
  133. $blog = Blog::where('slug', $slug)->first();
  134. return view("frontend.blog.details", compact('blog'));
  135. }
  136. }