Viewing File: /home/ubuntu/vedadeals-backend-base/app/Http/Controllers/Admin/FaqController.php

<?php

namespace App\Http\Controllers\Admin;
 
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use App\Helpers\Helper;

use DB, Exception, Setting;

use App\Models\{ Faq };

use App\Http\Requests\Admin\Faq\{ FaqPostRequest, FaqGetRequest };

class FaqController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(Request $request)
    {
        $this->middleware('auth:admin');

        $this->skip = $request->skip ?: 0;
       
        $this->take = $request->take ?: (Setting::get('admin_take_count') ?: TAKE_COUNT);
    }

    /**
     * @method faqs_index()
     *
     * @uses to list the faqs
     *
     * @created Karthick
     *
     * @updated  
     *
     * @param
     *
     * @return to faqs index page
     */

    public function faqs_index(Request $request) {

        $base_query = Faq::orderBy('created_at','DESC');

        if($request->filled('search_key')) {

                $faq_ids = Faq::when($request->filled('status'), function ($query) use ($request) {
                                        $query->where('status', $request->status);
                                    })->where(function($query) use ($request) {
                                        $query->where('title', "LIKE", "%". $request->search_key . "%");
                                        $query->orWhere('description', "LIKE", "%". $request->search_key . "%");
                                    })
                                    ->pluck('id');

                $base_query = $base_query->whereIn('id', $faq_ids);
            }

        if($request->filled('status')) {

            $base_query = $base_query->where('status', $request->status);
            
        } 

        $faqs = $base_query->paginate($this->take);
        
        return view('admin.faqs.index')
                    ->with('page', 'faqs')
                    ->with('sub_page', 'faqs-view')
                    ->with(compact(['faqs']));
    
    }

    /**
     * @method faqs_create()
     *
     * @uses To create a new faq
     *
     * @created  Karthick
     *
     * @updated 
     *
     * @param create a new object
     * 
     * @return to create faq page
     *
     */
    public function faqs_create() {

        try {

        $faq = new Faq;

        return view('admin.faqs.create')
                    ->with('page', 'faqs')
                    ->with('sub_page', 'faqs-create')
                    ->with(compact(['faq']));   

        } catch(Exception $e) {

            return redirect()->route('admin.faqs.index')->with('flash_error', $e->getMessage());
        }
    }

    /**
     * @method faqs_edit()
     *
     * @uses to edit faq details based on the faq_id
     *
     * @created Karthick
     *
     * @updated 
     *
     * @param object $request - faq_id
     * 
     * @return to edit faq page 
     *
     */
    public function faqs_edit(FaqGetRequest $request) {

        try {

            $faq = Faq::find($request->faq_id);

            return view('admin.faqs.edit')
                    ->with('page' , 'faqs')
                    ->with('sub_page','faqs-create')
                    ->with(compact(['faq']));
            
        } catch(Exception $e) {

            return redirect()->route('admin.faqs.index')->with('flash_error', $e->getMessage());
        }
    
    }

    /**
     * @method faqs_save()
     *
     * @uses to save the faq details of new/existing object based on faq_id
     *
     * @created Karthick
     *
     * @updated
     *
     * @param object $request - faq_id
     *
     * @return view faq page
     *
     */
    public function faqs_save(FaqPostRequest $request) {
            
        try {

            DB::begintransaction();

            $faq = Faq::updateOrCreate(['id' => $request->faq_id], $request->validated());

            if($faq) {

                DB::commit();

                $message = $faq->wasRecentlyCreated ? tr('faq_created_success') : tr('faq_updated_success');

                return redirect(route('admin.faqs.view', ['faq_id' => $faq->id ]))->with('flash_success', $message);

            }

            throw new Exception(tr('faq_save_failed'));
            
        } catch(Exception $e){ 

            DB::rollback();

            return back()->withInput()->with('flash_error', $e->getMessage());

        } 

    }

    /**
     * @method faqs_view()
     *
     * @uses to view the faq details based on faq_id
     *
     * @created Karthick 
     *
     * @updated 
     *
     * @param object $request - faq_id
     * 
     * @return View page
     *
     */
    public function faqs_view(FaqGetRequest $request) {
       
        try {
      
            $faq = Faq::find($request->faq_id);   

            return view('admin.faqs.view')
                        ->with('page', 'faqs')
                        ->with('sub_page', 'faqs-view')
                        ->with('faq',$faq);
            
        } catch (Exception $e) {

            return redirect()->route('admin.faqs.index')->with('flash_error', $e->getMessage());
        }
    
    }


    /**
     * @method faqs_status
     *
     * @uses to change faq status as DECLINED/APPROVED based on faq_id
     *
     * @created Karthick
     *
     * @updated 
     *
     * @param object $request - faq_id
     * 
     * @return response success/failure message
     *
     **/
    public function faqs_status(FaqGetRequest $request) {

        try {

            DB::beginTransaction();

            $faq = Faq::find($request->faq_id);

            $result = $faq->update(['status' => $faq->status ? DECLINED : APPROVED]);

            if($result) {

                DB::commit();

                $message = $faq->status ? tr('faq_approve_success') : tr('faq_decline_success');

                return back()->with('flash_success', $message);
            }
            
            throw new Exception(tr('faq_status_change_failed'));

        } catch(Exception $e) {

            DB::rollback();

            return redirect()->route('admin.faqs.index')->with('flash_error', $e->getMessage());

        }

    }

    /**
     * @method faqs_delete()
     *
     * @uses to delete the faq details based on faq_id
     *
     * @created Karthick 
     *
     * @updated  
     *
     * @param object $request - faq_id
     * 
     * @return response of success/failure details with view page
     *
     */
    public function faqs_delete(FaqGetRequest $request) {

        try {

            DB::begintransaction();

            $faq = Faq::find($request->faq_id);

            if($faq->delete()) {

                DB::commit();

                return redirect()->route('admin.faqs.index')->with('flash_success', tr('faq_deleted_success'));   

            } 
            
            throw new Exception(tr('faq_delete_failed'));
            
        } catch(Exception $e){

            DB::rollback();

            return back()->with('flash_error', $e->getMessage());

        }       
         
    }
}
Back to Directory File Manager