Current File : /home/tradevaly/www/app/Http/Controllers/User/AdvertismentController.php
<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\Banner;
use Brian2694\Toastr\Facades\Toastr;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Auth;
use App\Models\Category;

class AdvertismentController extends Controller
{
public function index(){
    $data['advertisments'] = Banner::where('user_id',Auth::id())->latest()->paginate(12);
    return view('user.pages.advertisment.index')->with($data);
}
public function pending(){
    $data['advertisments'] = Banner::where('user_id',Auth::id())->where('isActive',0)->latest()->paginate(12);
    return view('user.pages.advertisment.pending')->with($data);
}
    public function edit($id){
        $data['banner'] = Banner::where('id',$id)->where('user_id',Auth::id())->firstOrFail();
        $data['categories'] = Category::where('parent_id' , 0)->latest()->get();
        return view('user.pages.advertisment.edit')->with($data);
       
    }
    
   
    public function update(Request $request ,$id){
        $request->validate([
            'link' => 'required',
            'image' => ['nullable', 'file', 'max:3120'],
            ], [
        'image.max' => 'The :attribute must be smaller than 3MB.',
    ]);
            
        $data['banner'] = Banner::where('id',$id)->where('user_id',Auth::id())->firstOrFail();;
        $data['categories'] = Category::where('parent_id' , 0)->latest()->get();


             if($request->hasFile('image'))
             {
    
             $image = $request->file('image');
             $currentDate = Carbon::now()->toDateString();
             $imageName = $currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
             if(Storage::disk('public')->exists('images/admin/banner/'.$request->image))
             {
                Storage::disk('public')->delete('images/admin/banner/'.$request->image);
             }
                    if($image->getClientOriginalExtension() == 'gif'){
                            Storage::disk('public')->put('images/admin/banner/',$image); 
                          $imageName = $image->hashName();
                     }
                     else{
                      
                           $bannerImage = Image::make($image)->resize(840,418)->stream();
                            Storage::disk('public')->put('images/admin/banner/'.$imageName,$bannerImage); 
                     }
             
             
             $data['banner']->image = $imageName;
             }
             if($request->category_id){
                $data['banner']->category_id = $request->category_id;
            }
             $data['banner']->link = $request->link;
             $data['banner']->save();
             Toastr::success('Success!','Banner Updated successfully!');
             return back();
       
    }
    
     public function renew_rent(Request $request,$id){
        $banner = Banner::where('id',$id)->where('user_id',Auth::id())->firstOrFail();
        
         $slug = $banner->position;
        $discount_name = $slug.'_discount';
       $duration = $request->duration;
       $amount = settings()->$slug;
      
       $discount = settings()->$discount_name;
        // 
        // dd($discount_name,$duration,$amount,$slug);
        $data['categories'] = Category::where('parent_id' , 0)->latest()->get();
        return view('user.pages.advertisment.renew')->with($data);
       
    }
    public function expired()
    {
            $data['advertisments'] = Banner::where('user_id',Auth::id())
                ->whereNotNull('link')
                ->whereDate('end', '<=', Carbon::now()) 
                ->latest()->get();
            return view('user.pages.advertisment.expired')->with($data);
    }
    public function renew($id){
        $data['banner'] = Banner::where('id',$id)->where('user_id',Auth::id())->firstOrFail();
        $data['categories'] = Category::where('parent_id' , 0)->latest()->get();
        return view('user.pages.advertisment.renew')->with($data);
    }
    
    public function renewUpdate($data)
    {
        dd ($data);
    }

}