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

namespace App\Http\Controllers;

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

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

        return view('backend.abouts.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.abouts.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, [
            'title' => 'required',

            'total_country' => 'required',
            
            'total_client' => 'required',
            
            'total_business' => 'required',
            
            'total_register_user' => 'required',
            
            'total_visitor_daily' => 'required',
            
            'total_review' => 'required',
            
            'description' => 'required',

           // 'banner_image' => 'required|image|mimes:jpeg,png,jpg|max:512',
            
        ]);

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


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

           
        
        }


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

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

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

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

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

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\About  $about
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, About $about)
    {
         $input = $request->all();
        
        $this->validate($request, [
            'title' => 'required',

            'total_country' => 'required',
            
            'total_client' => 'required',
            
            'total_business' => 'required',
            
            'total_register_user' => 'required',
            
            'total_visitor_daily' => 'required',
            
            'total_review' => 'required',
            
            'description' => 'required',

            //'banner_image' => 'image|mimes:jpeg,png,jpg|max:512',
        ]);

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

           if( !empty($about->banner_image) ){

                
                if( file_exists('storage/app/public/abouts/'.$about->banner_image) ){
                    
                    unlink('storage/app/public/abouts/'.$about->banner_image);
                
                }
           }
        
        }

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

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

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

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

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

           
            $responseStatus = 'success';
            $responseMessage = 'About deleted successfully.';

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

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


     public function activeInactive($id)
    {
      

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

        //dd($data;die;

 
        $responseMessage = '';

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

            $input['status'] = 1;

            $responseMessage = 'About active successfully';

        }else{

            $input['status'] = 0;

            $responseMessage = 'About inactive successfully';
        }



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

            $responseStatus = 'success';

        }else{

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

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

}