관리-도구
편집 파일: BlogController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Blog; use App\Models\BlogCategory; class BlogController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $data = Blog::where('is_active',1)->simplePaginate(9); return view('backend.blogs.index',['data'=>$data]); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create(Request $request) { // } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $data=$request->except('_token'); if($request->hasFile('image')){ foreach($request->file('image') as $image) { $name=$image->getClientOriginalName(); $image->move(public_path().'/theme/default/images/', $name); $img[] = $name; } $data['image'] = json_encode($img); } $save = Blog::create($data); if($save){ return redirect(route('blogcms'))->with("success", "Blog has been created Successfully"); }else{ return redirect(route('blogcms'))->with("error","Sorry! Blog couldn't be created!"); } } /** * 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) { $productsData = Blog::all()->where('id',(int)$id); $singleData= ''; foreach($productsData as $sData) { $singleData = $sData; } return view('backend.blogs.show',['singleData'=>$singleData,'id'=>$id]); } public function showCreateForm() { return view('backend.blogs.createform'); } public function getBlogs() { $blogs = Blog::where('is_active',1)->limit(8)->get(); return $blogs; } public function getLatestBlogs() { $latestBlogs = Blog::where('is_active',1)->orderby('created_at','desc')->limit(2)->get(); return $latestBlogs; } static public function getRandomBlogs() { $randomBlogs = Blog::where('is_active',1)->orderby('created_at','desc')->skip(1)->take(4)->get(); return $randomBlogs; } static public function getBlogCategories() { $blogCategory = BlogCategory::orderby('created_at','desc')->get(); return $blogCategory; } public function singleBlog($id) { $singleBlog = Blog::where('id',$id)->first(); return view('backend.blogs.single-blog',['singleBlog'=>$singleBlog]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request) { $data=$request->all(); $result=Blog::find($request->id); if($request->hasFile('image')){ foreach($request->file('image') as $image) { $name=$image->getClientOriginalName(); $image->move(public_path().'/theme/default/images/', $name); $img[] = $name; } $data['image'] = json_encode($img); } $save=$result->fill($data)->save(); if($save){ return redirect(route('blogcms'))->with("success",'Data Updated Succesfully'); }else{ return redirect(route('blogcms'))->with("error",'Error Ocuured Data is not Updated!'); } } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $result = Blog::where('id',$id)->delete(); if($result){ return redirect(route('blogcms'))->with("success",'Blog Removed Succesfully'); }else{ return redirect(route('blogcms'))->with("error",'Error Ocuured Blog is not Removed!'); } } }