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

namespace App\Http\Controllers;

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

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

        return view('backend.durations.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()
    {
        $data = Duration::pluck('name','name')->all();

        return view('backend.durations.create',compact('data'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|digits_between:2,3|numeric|unique:durations,name',
        ]);

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



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

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

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

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

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

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Duration  $duration
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Duration $duration)
    {
        
        $input = $request->all();
        
        $this->validate($request, [
            'name' => 'required|digits_between:2,3|numeric|unique:durations,name,'.$duration->id,
        ]);

       
        $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");;

      

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

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

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

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

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

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

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

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


    public function activeInactive($id)
    {
      

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

        //dd($data;die;

 
        $responseMessage = '';

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

            $input['status'] = 1;

            $responseMessage = 'Duration active successfully';

        }else{

            $input['status'] = 0;

            $responseMessage = 'Duration 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('durations.index')
                        ->with($responseStatus, $responseMessage);
    }

}