Current File : //home/tradevaly/www/app/Http/Controllers/Admin/NewsController.php
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\News;
use App\Models\Logo;
use Brian2694\Toastr\Facades\Toastr;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

class NewsController extends Controller
{
    public function index(){
        $khobor = News::first();
        return view('backend.pages.news.index')->with('khobor',$khobor);
    }
    
    public function delete($id){
        $data= News::find($id);
        $data->delete();
        Toastr::success('Deleted!');
        return redirect()->back();
    }
    public function discount(){
        return view('backend.pages.news.discount_edit');
    }
    public function add(Request $request){
        if($request->isMethod("post")){
            $request->validate([
               'title'=>'required',
               'status'=>'required',
            ]);
            $data=new News;
            $data->title=$request->title;
            $data->status=$request->status;
            $data->save();
            Toastr::success('Success!','Topbar Added!');
            return redirect()->back();
        }
        return view('backend.pages.news.add');
       
    }

    public function edit(Request $request ,$id){
        $news=News::find($id);
        if($request->isMethod("post")){
            $news->title=$request->title;
            $news->status=$request->status;
            $news->save();
             Toastr::success('Success!','Updated successfully!');
             return redirect()->back(); die;
        }
        return view('backend.pages.news.edit')->with('news',$news);
       
    }

    public function discount_update(Request $request){
        if($request->isMethod("post")){
            $discount = Logo::first();
            if($request->hasFile('discount_img'))
            {
                $image = $request->file('discount_img');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
                if(!Storage::disk('public')->exists('images/admin/discount/'.$discount->discount_img))
                {

                    Storage::disk('public')->makeDirectory('images/admin/discount/'.$discount->discount_img);
                }
                if($image->getClientOriginalExtension() == 'gif'){

                    Storage::disk('public')->put('images/admin/banner/',$image); 
                    
                    $imageName = $image->hashName();
                }
                else{
                    $bannerImage = Image::make($image)->resize(1000,90)->stream();
                    Storage::disk('public')->put('images/admin/banner/'.$imageName,$bannerImage); 
                    $data->image= "default.png";
                }
            }
          
            $discount->discount_link=$request->discount_link;
            $discount->discount_status=$request->discount_status;
            $discount->save();
            Toastr::success('Success!','Updated successfully!');
            return redirect()->back(); die;
        }
        return back();
    }


}