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

namespace App\Http\Controllers;

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

use Storage;

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

        return view('backend.recommends.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.recommends.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',
            'banner_image' => 'required|image|mimes:jpeg,png,jpg|dimensions:width=1921,height=374',
            'recommend_logo' => 'required|image|mimes:jpeg,png,jpg|dimensions:width=560,height=195|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 = 'banner_image'.date('Ymdhis').$imagePath->getClientOriginalName();
          
           $path = $input['banner_image']->storeAs('recommends', $imageName, 'public');
        
           $input['banner_image'] = $imageName;

           
        
        }

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

           
        
        }

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

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

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

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

    }

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

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

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

        $input = $request->all();
        
        $this->validate($request, [
            'title' => 'required',
            'banner_image' => 'image|mimes:jpeg,png,jpg|dimensions:width=1921,height=374',
            'recommend_logo' => 'image|mimes:jpeg,png,jpg|dimensions:width=560,height=195|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 = 'banner_image'.date('Ymdhis').$imagePath->getClientOriginalName();
          
           $path = $input['banner_image']->storeAs('recommends', $imageName, 'public');
        
           $input['banner_image'] = $imageName;

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

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



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

           if( !empty($recommend->recommend_logo) ){

                if( file_exists('storage/app/public/recommends/'.$recommend->recommend_logo) ){
                
                    unlink('storage/app/public/recommends/'.$recommend->recommend_logo);
                
                }
           }
        
        }


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

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

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

    }

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

        $data = Recommend::find($recommend->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/Recommend/'.$data->image) ){
                
                    unlink('public/storage/Recommend/'.$data->image);
                
                }
            }*/

            $responseStatus = 'success';
            $responseMessage = 'Company Recommend deleted successfully.';

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

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

    }



    public function activeInactive($id)
    {
      

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

        //dd($data;die;

 
        $responseMessage = '';

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

            $input['status'] = 1;

            $responseMessage = 'Recommend active successfully';

        }else{

            $input['status'] = 0;

            $responseMessage = 'Recommend inactive successfully';
        }



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

            $responseStatus = 'success';

        }else{

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

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




}