Current File : /home/tradevaly/new.tradevaly.com.bd/app/Http/Controllers/Admin/BannerController.php |
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Banner;
use App\Models\Category;
use Brian2694\Toastr\Facades\Toastr;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
class BannerController extends Controller
{
public function index(){
$data['categories']= Banner::where('position','top_category_banner')->orWhere('position','bottom_category_banner')->latest()->get();
$data['buyers']= Banner::where('position','top_buyer_banner')->orWhere('position','bottom_buyer_banner')->latest()->get();
$data['products']= Banner::where('position','top_product_banner')->orWhere('position','bottom_product_banner')->latest()->get();
$data['companies']= Banner::where('position','top_company_banner')->orWhere('position','bottom_company_banner')->latest()->get();
$data['manufactures']= Banner::where('position','top_manufacture_banner')->orWhere('position','bottom_manufacture_banner')->latest()->get();
$data['contacts']= Banner::where('position','contact-banner')->get();
$data['banners']= Banner::Where('position','slider')->orWhere('position','after-banner-left')->orWhere('position','after-banner-left-center')->orWhere('position','after-banner-right-center')->orWhere('position','after-banner-right')->orWhere('position','after-category-left')->orWhere('position','after-category-right')->latest()->get();
return view('backend.pages.Banner.index')->with($data);
}
public function buyers(){
$data['banners'] = Banner::where('position','top_buyer_banner')->orWhere('position','bottom_buyer_banner')->latest()->get();
return view('backend.pages.Banner.buyers')->with($data);
}
public function product(){
$data['banners'] = Banner::where('position','top_product_banner')->orWhere('position','bottom_product_banner')->latest()->get();
return view('backend.pages.Banner.product')->with($data);
}
public function contact(){
$data['banners'] = Banner::where('position','contact-banner')->latest()->get();
return view('backend.pages.Banner.contact')->with($data);
}
public function company(){
$data['banners'] = Banner::where('position','top_company_banner')->orWhere('position','bottom_company_banner')->latest()->get();
return view('backend.pages.Banner.company')->with($data);
}
public function manufacture(){
$data['banners'] = Banner::where('position','top_manufacture_banner')->orWhere('position','bottom_manufacture_banner')->latest()->get();
return view('backend.pages.Banner.manufacture')->with($data);
}
public function category(){
$data['banners'] = Banner::where('position','top_category_banner')->orWhere('position','bottom_category_banner')->latest()->get();
return view('backend.pages.Banner.category')->with($data);
}
public function updateBannerStatus(Request $request){
$banner = Banner::find($request->banner_id);
if($request->ajax()){
$data= $request->all();
if($banner->status==1){
$status=0;
}else{
$status=1;
}
$banner->status = $status;
$banner->save();
return response()->json(['status'=>$status, 'id'=>$data['banner_id']]);
}
}
public function delete($id){
$data= Banner::find($id);
if(Storage::disk('public')->exists('images/admin/banner/'.$data->image))
{
Storage::disk('public')->delete('images/admin/banner/'.$data->image);
}
$data->delete();
Toastr::success('Deleted!');
return redirect()->back();
}
public function add(Request $request){
if($request->isMethod("post")){
$request->validate([
'image'=>'required|mimes:png,jpg,jpeg,webp,gif|max:5000',
'link'=>'required',
]);
$data=new Banner;
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/'.$data->image))
{
Storage::disk('public')->makeDirectory('images/admin/banner/'.$data->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);
}
// $bannerImage = Image::make($image)->resize(840,418)->stream();
// Storage::disk('public')->put('images/admin/banner/'.$imageName,$bannerImage);
$data->image=$imageName;
}else{
$data->image= "default.png";
}
$data->link=$request->link;
$data->position=$request->position;
if($request->category_id){
$data->category_id = $request->category_id;
}
$data->status=1;
$data->save();
Toastr::success('Success!','Banner Uploaded!');
return redirect()->back();
}
$data['categories'] = Category::where('parent_id' , 0)->latest()->get();
return view('backend.pages.Banner.add')->with($data);
}
public function edit(Request $request ,$id){
$data['banner']=Banner::find($id);
$data['categories'] = Category::where('parent_id' , 0)->latest()->get();
if($request->isMethod("post")){
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!','Updated successfully!');
return back();
}
return view('backend.pages.Banner.edit')->with($data);
}
public function home_page_banner(Request $request ,$id){
$banner = Banner::findOrFail($id);
$data['categories'] = Category::where('parent_id' , 0)->latest()->get();
if($request->isMethod("post")){
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);
}
$banner->image=$imageName;
}
$banner->link=$request->link;
$banner->position=$request->position;
$banner->status=1;
$banner->save();
Toastr::success('Success!','Banner Updated successfully!');
return redirect('b2b/component/banner/index');
}
$data['banner']=$banner;
// die();
return view('backend.pages.Banner.home_banner_edit')->with($data);
}
}