mInfografisController.php 5.35 KB
<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Yajra\DataTables\Facades\DataTables;
use Illuminate\Support\Facades\Validator;
use App\Models\Infografis;
use App\Models\Peraturan;
use Illuminate\Support\Facades\Storage;

class mInfografisController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view ('backend.infografis.index');
    }

    public function data(Request $request)
    {
        if ($request->ajax()) {
            $infografis  = Infografis::with('peraturan') -> latest('id');
            return Datatables::of($infografis)

            ->addIndexColumn()
            ->addColumn('lampiran', function($infografis){
                return '<a href="'. $infografis->file_url . '">Download</a>';
            })

            ->addColumn(
                'action',
                            '<center>
                              <a class="edit ubah" data-toggle="tooltip" data-placement="top" title="Edit" infografis-id="{{ $id }}">
                                  <i class="fa fa-pencil text-warning"></i>
                              </a>&nbsp &nbsp
                              <a class="delete hidden-xs hidden-sm hapus" data-toggle="tooltip" data-placement="top" title="Delete" infografis-id="{{ $id }}">
                                  <i class="fa fa-trash text-danger"></i>
                              </a>
                            </center>'
            )

            ->rawColumns(['action', 'lampiran'])->make(true);

        } else {
            exit("Not an AJAX request -_-");
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $peraturan_id = Peraturan::pluck('nama','id');
        return view('backend.infografis.tambah', compact('peraturan_id'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'judul'         => 'required',
          ]);

       if ($validator->fails()) {
            $respon = array('status'=>false, 'pesan' => $validator->messages());
                } else {
            $path       = NULL;
            $namafile   = NULL;

            if($request->hasFile('lampiran')){
                $file = Storage::putFile('infografis/'.date('Y').'/'.date('m').'/'.date('d'),$request->file('lampiran'));

          $request->request->add([
               'file'  =>  [
                                'disk'      => config('filesystems.default'),
                                'target'    => $file,
                            ],
         ]);
       }
            if (infografis::create($request->all())) {
                $respon = array('status'=>true, 'pesan' => ['msg' => 'Data berhasil disimpan']);
                        } else {
                $respon = array('status'=>false, 'pesan' => ['msg' => 'Data gagal disimpan']);
          }
     }
        return response()->json($respon);
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $infografis = Infografis::find($id);
        $peraturan_id = Peraturan::pluck('nama', 'id');

        return view ('backend.infografis.ubah', compact('infografis','peraturan_id'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'judul'                => 'required',

        ]);

        if ($validator->fails()) {
            $respon = array('status'=>false, 'pesan' => $validator->messages());
        } else {
            $infografis = Infografis::find($id);
                $update = $infografis->update($request->all());
            if ($update) {
                $respon = array('status'=>true, 'pesan' => ['msg' => 'Data berhasil diubah']);
            } else {
                $respon = array('status'=>false, 'pesan' => ['msg' => 'Data gagal diubah']);
            }
        }
        return response()->json($respon);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

     public function hapus($id)
     {
         $infografis = Infografis::find($id);

         return view ('backend.infografis.hapus', compact('infografis'));
     }
    public function destroy($id)
    {
        $infografis = Infografis::find($id);
        if ($infografis->delete()) {
            $respon = array('status'=>true, 'pesan' => ['msg' => 'Data berhasil dihapus']);
        } else {
            $respon = array('status'=>false, 'pesan' => ['msg' => 'Data gagal dihapus']);
        }
        return response()->json($respon);
    }
}