123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace App\Http\Controllers;
- use App\Http\Controllers\Controller;
- use Illuminate\Http\Request;
- use App\Models\BlogCategory;
- use App\Models\Blog;
- class BlogController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index(Request $request)
- {
- $sort_search = null;
- $blogs = Blog::orderBy('created_at', 'desc');
-
- if ($request->search != null){
- $blogs = $blogs->where('title', 'like', '%'.$request->search.'%');
- $sort_search = $request->search;
- }
- $blogs = $blogs->paginate(15);
- return view('backend.blog_system.blog.index', compact('blogs','sort_search'));
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- $blog_categories = BlogCategory::all();
- return view('backend.blog_system.blog.create', compact('blog_categories'));
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
-
- $request->validate([
- 'category_id' => 'required',
- 'title' => 'required|max:255',
- ]);
- $blog = new Blog;
-
- $blog->category_id = $request->category_id;
- $blog->title = $request->title;
- $blog->banner = $request->banner;
- $blog->slug = preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $request->slug));
- $blog->short_description = $request->short_description;
- $blog->description = $request->description;
-
- $blog->meta_title = $request->meta_title;
- $blog->meta_img = $request->meta_img;
- $blog->meta_description = $request->meta_description;
- $blog->meta_keywords = $request->meta_keywords;
-
- $blog->save();
- flash(translate('Blog post has been created successfully'))->success();
- return redirect()->route('blog.index');
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
-
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit($id)
- {
- $blog = Blog::find($id);
- $blog_categories = BlogCategory::all();
-
- return view('backend.blog_system.blog.edit', compact('blog','blog_categories'));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- $request->validate([
- 'category_id' => 'required',
- 'title' => 'required|max:255',
- ]);
- $blog = Blog::find($id);
- $blog->category_id = $request->category_id;
- $blog->title = $request->title;
- $blog->banner = $request->banner;
- $blog->slug = preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $request->slug));
- $blog->short_description = $request->short_description;
- $blog->description = $request->description;
-
- $blog->meta_title = $request->meta_title;
- $blog->meta_img = $request->meta_img;
- $blog->meta_description = $request->meta_description;
- $blog->meta_keywords = $request->meta_keywords;
-
- $blog->save();
- flash(translate('Blog post has been updated successfully'))->success();
- return redirect()->route('blog.index');
- }
-
- public function change_status(Request $request) {
- $blog = Blog::find($request->id);
- $blog->status = $request->status;
-
- $blog->save();
- return 1;
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- Blog::find($id)->delete();
-
- return redirect('admin/blogs');
- }
- public function all_blog() {
- $blogs = Blog::where('status', 1)->orderBy('created_at', 'desc')->paginate(12);
- return view("frontend.blog.listing", compact('blogs'));
- }
-
- public function blog_details($slug) {
- $blog = Blog::where('slug', $slug)->first();
- return view("frontend.blog.details", compact('blog'));
- }
- }
|