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

namespace App\Http\Controllers;

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

class WhyTradevalyController extends Controller
{
    public function index(Request $request)
    {
        $data = WhyTradevaly::orderBy('id','DESC')->paginate(10);

        return view('backend.why-tradevalies.index',compact('data'))
            ->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.why-tradevalies.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, [
            'video' => 'required',
            'description' => 'required',
            
            'thumble1_title' => 'required',
            'thumble2_title' => 'required',
            'thumble3_title' => 'required',
            'thumble4_title' => 'required',

            'thumble1_image' => 'required|image|mimes:jpeg,png,jpg|dimensions:width=250,height=214',
            'thumble2_image' => 'required|image|mimes:jpeg,png,jpg|dimensions:width=250,height=214|max:512',
            'thumble3_image' => 'required|image|mimes:jpeg,png,jpg|dimensions:width=250,height=214|max:512',
            'thumble4_image' => 'required|image|mimes:jpeg,png,jpg|dimensions:width=250,height=214|max:512',

            'thumble1_description' => 'required',
            'thumble2_description' => 'required',
            'thumble3_description' => 'required',
            'thumble4_description' => 'required',
        ]);

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

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

        
        }


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

        
        }
        

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

        
        }

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

        
        }


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

        if( WhyTradevaly::create($input) ) {
            $responseStatus = 'success';
            $responseMessage = 'Advertise cause added successfully.';

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

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

    }

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

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\WhyTradevaly  $WhyTradevaly
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, WhyTradevaly $WhyTradevaly)
    {
        

        $input = $request->all();
        
        $this->validate($request, [
            'video' => 'required',
            'description' => 'required',
            
            'thumble1_title' => 'required',
            'thumble2_title' => 'required',
            'thumble3_title' => 'required',
            'thumble4_title' => 'required',

            'thumble1_image' => 'image|mimes:jpeg,png,jpg|dimensions:width=250,height=214',
            'thumble2_image' => 'image|mimes:jpeg,png,jpg|dimensions:width=250,height=214|max:512',
            'thumble3_image' => 'image|mimes:jpeg,png,jpg|dimensions:width=250,height=214|max:512',
            'thumble4_image' => 'image|mimes:jpeg,png,jpg|dimensions:width=250,height=214|max:512',

            'thumble1_description' => 'required',
            'thumble2_description' => 'required',
            'thumble3_description' => 'required',
            'thumble4_description' => 'required',
        ]);

       
        $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['thumble1_image']) ) {
            
           $imagePath = $input['thumble1_image'];
           $imageName = date('Ymdhis').$imagePath->getClientOriginalName();
          
           $path = $input['thumble1_image']->storeAs('why-tradevalies', $imageName, 'public');
        
           $input['thumble1_image'] = $imageName;

           if( !empty($WhyTradevaly->thumble1_image) ){

                if( file_exists('storage/app/public/why-tradevalies/'.$WhyTradevaly->thumble1_image) ){
                
                    unlink('storage/app/public/advertise-causes/'.$WhyTradevaly->thumble1_image);
                
                }
           }
        
        }


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

           if( !empty($WhyTradevaly->thumble2_image) ){

                if( file_exists('storage/app/public/why-tradevalies/'.$WhyTradevaly->thumble2_image) ){
                
                    unlink('storage/app/public/advertise-causes/'.$WhyTradevaly->thumble2_image);
                
                }
           }
        
        }


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

           if( !empty($WhyTradevaly->thumble3_image) ){

                if( file_exists('storage/app/public/why-tradevalies/'.$WhyTradevaly->thumble3_image) ){
                
                    unlink('storage/app/public/advertise-causes/'.$WhyTradevaly->thumble3_image);
                
                }
           }
        
        }


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

           if( !empty($WhyTradevaly->thumble3_image) ){

                if( file_exists('storage/app/public/why-tradevalies/'.$WhyTradevaly->thumble4_image) ){
                
                    unlink('storage/app/public/advertise-causes/'.$WhyTradevaly->thumble4_image);
                
                }
           }
        
        }



        $data = WhyTradevaly::find($WhyTradevaly->id);
        
        if( $data->update($input) ) {
            $responseStatus = 'success';
            $responseMessage = 'Advertise Cause  updated successfully.';

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

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

    }

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

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

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


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

            $responseStatus = 'success';
            $responseMessage = 'Company Advertise cause deleted successfully.';

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

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

    }



    public function activeInactive($id)
    {
      

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

        //dd($data;die;

 
        $responseMessage = '';

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

            $input['status'] = 1;

            $responseMessage = 'Advertise cause active successfully';

        }else{

            $input['status'] = 0;

            $responseMessage = 'Advertise cause inactive successfully';
        }



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

            $responseStatus = 'success';

        }else{

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

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




}