Current File : /home/tradevaly/demo.tradevaly.com.bd/app/Http/Controllers/IndexSlideController.php
<?php

namespace App\Http\Controllers;

use App\Models\IndexSlide;
use Illuminate\Http\Request;
use Auth;

class IndexSlideController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $data = IndexSlide::orderBy('id','DESC')->paginate(10);
        $countData = IndexSlide::orderBy('id','DESC')->count();

        return view('backend.index-slides.index',compact('data', 'countData'))
            ->with('i', ($request->input('page', 1) - 1) * 10);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        
        return view('backend.index-slides.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            
            'text_one' => 'required',
            'text_two' => 'required',
            'text_three' => 'required',
            'text_four' => 'required',
            'image' => 'image|mimes:jpeg,jpg|dimensions:width=2500,height=1667',
        ]);

       
        $input = $request->all();
        $input['created_user_id'] = !empty(Auth::user()->id) ? Auth::user()->id : 0;
        $input['updated_at'] = Null;


        if ( !empty($input['image']) ) {
            
           $imagePath = $input['image'];
           $imageName = date('Ymdhis').$imagePath->getClientOriginalName();
          
           $path = $input['image']->storeAs('index-slides', $imageName, 'public');
        
           $input['image'] = $imageName;

           
        
        }

        $responseStatus = '';
        $responseMessage = '';

        if( IndexSlide::create($input) ) {
            $responseStatus = 'success';
            $responseMessage = 'Country added successfully.';

        }else{
            $responseStatus = 'error';
            $responseMessage = 'Something went wrong, please try again.';
       
        }

        //$add = CompanyIndexSlide::create(['name' => $request->input('name')]);
    
        return redirect()->route('index-slides.index')
                        ->with($responseStatus, $responseMessage);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\IndexSlide  $indexSlide
     * @return \Illuminate\Http\Response
     */
    public function show(IndexSlide $indexSlide)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\IndexSlide  $indexSlide
     * @return \Illuminate\Http\Response
     */
    public function edit(IndexSlide $indexSlide)
    {
        $data = IndexSlide::find($indexSlide->id);
        
        return view('backend.index-slides.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\IndexSlide  $indexSlide
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, IndexSlide $indexSlide)
    {
        $input = $request->all();
        
        $this->validate($request, [
            'text_one' => 'required',
            'text_two' => 'required',
            'text_three' => 'required',
            'text_four' => 'required',
            'image' => 'image|mimes:jpeg,jpg|dimensions:width=2500,height=1667',
        ]);

       
        $input = $request->all();
        $input['modified_user_id'] = !empty(Auth::user()->id) ? Auth::user()->id : 0;
        $input['updated_at'] = date("Y-m-d h:i:sa");;

       if ( !empty($input['image']) ) {
            
           $imagePath = $input['image'];
           $imageName = date('Ymdhis').$imagePath->getClientOriginalName();
          
           $path = $input['image']->storeAs('index-slides', $imageName, 'public');
        
           $input['image'] = $imageName;

           if( !empty($indexSlide->image) ){


                if( file_exists('storage/app/public/index-slides/'.$indexSlide->image) ){
                
                    unlink('storage/app/public/index-slides/'.$indexSlide->image);
                
                }
           }
        
        }

        $data = IndexSlide::find($indexSlide->id);
        
        if( $data->update($input) ) {
            $responseStatus = 'success';
            $responseMessage = 'Index slide updated successfully.';

        }else{
            $responseStatus = 'error';
            $responseMessage = 'Something went wrong, please try again.';
       
        }

        //$add = CompanyIndexSlide::create(['name' => $request->input('name')]);
    
        return redirect()->route('index-slides.index')
                        ->with($responseStatus, $responseMessage);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\IndexSlide  $indexSlide
     * @return \Illuminate\Http\Response
     */
    public function destroy(IndexSlide $indexSlide)
    {
        $update['deleted_user_id'] =  !empty(Auth::user()->id) ? Auth::user()->id : 0;

        $data = IndexSlide::find($indexSlide->id);
        
        if( $data->delete() && $data->update([ 'deleted_user_id' => !empty(Auth::user()->id) ? Auth::user()->id : 0]) ) {

            /*if( !empty($data->flag) ){


                if( file_exists('public/storage/country/'.$data->flag) ){
                
                    unlink('public/storage/country/'.$data->flag);
                
                }
            }*/

            $responseStatus = 'success';
            $responseMessage = 'Index slide deleted successfully.';

        }else{
            $responseStatus = 'error';
            $responseMessage = 'Something went wrong, please try again.';
       
        }

        //$add = CompanyIndexSlide::create(['name' => $request->input('name')]);
    
        return redirect()->route('index-slides.index')
                        ->with($responseStatus, $responseMessage);

    }





    public function activeInactive($id)
    {
      

        $data = IndexSlide::findOrFail($id);

        //dd($data;die;

 
        $responseMessage = '';

        if($data->status == 0){

            $input['status'] = 1;

            $responseMessage = 'Country active successfully';

        }else{

            $input['status'] = 0;

            $responseMessage = 'Country inactive successfully';
        }



        if( $data->update($input) ) {

            $responseStatus = 'success';

        }else{

            $responseStatus = 'error';
            $responseMessage = 'Something went wrong, please try again.';
       
        }

        //$add = IndexSlide::create(['name' => $request->input('name')]);
    
        return redirect()->route('index-slides.index')
                        ->with($responseStatus, $responseMessage);
    }



}