Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
4.4 kB
0
Indexable
Never
//fasilitas controller
<?php


namespace App\Http\Controllers\Fasiliti;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Fasilitas;
use Illuminate\Support\Facades\Validator;
use CloudinaryLabs\CloudinaryLaravel\Facades\Cloudinary;

class FasilitasController extends Controller
{
    public function index()
    {
        $fasilitas = Fasilitas::paginate(10); // Menampilkan 10 item per halaman
        return view('fasiliti.index', compact('fasilitas'));
    }

    public function create()
    {
        return view('fasiliti.tambah'); // Pastikan tampilan sudah ada
    }

    public function store(Request $request)
    {
        // Validasi inputan
        $validator = Validator::make($request->all(), [
            'nama' => 'required|string|max:255',
            'keterangan' => 'required|string|max:1000',
            'status' => 'required|in:1,0',
            'gambar' => 'nullable|image|mimes:jpeg,png|max:8192', // Ukuran maksimum 8MB
        ]);
    
        // Jika validasi gagal, kembalikan dengan pesan kesalahan
        if ($validator->fails()) {
            return back()->withErrors($validator)->withInput(); 
        }
    
        // Siapkan data untuk disimpan
        $data = [
            'nama' => $request->input('nama'),
            'keterangan' => $request->input('keterangan'),
            'status' => (bool) $request->input('status'), // Konversi ke boolean
        ];
    
        // Jika ada file gambar yang diunggah
        if ($request->hasFile('cover')) {
            $file = $request->file('cover');
            $path = '/img/kempen';
            $fileName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
            $publicId = date('Y-m-d_His') . '_' . $fileName;
    
            // Unggah ke Cloudinary
            $upload = Cloudinary::upload(
                $file->getRealPath(),
                [
                    "public_id" => $publicId,
                    "folder" => $path
                ]
            );
    
            $data['gambar'] = $upload->getSecurePath(); // Simpan URL gambar
        }
    
        // Simpan data ke dalam database
        Fasilitas::create($data);
    
        // Kembalikan ke rute fasilitas dengan pesan sukses
        return redirect()->route('fasiliti.index')->with('success', 'Fasilitas berhasil ditambahkan.');
    }

    public function edit($id)
{
    // Cari fasilitas berdasarkan ID
    $fasilitas = Fasilitas::find($id);
    if (!$fasilitas) {
        return redirect()->route('fasiliti.index')->with('error', 'Fasilitas tidak ditemukan.');
    }

    // Tampilkan view edit dengan data fasilitas
    return view('fasiliti.edit', compact('fasilitas'));
}

public function update(Request $request, $id)
{
    // Validasi inputan
    $validator = Validator::make($request->all(), [
        'nama' => 'required|string|max:255',
        'keterangan' => 'required|string|max:1000',
        'status' => 'required|in:1,0',
        'gambar' => 'nullable|image|mimes:jpeg,png|max:8192', // Ukuran maksimum 8MB
    ]);

    if ($validator->fails()) {
        return back()->withErrors($validator)->withInput();
    }

    // Cari fasilitas berdasarkan ID
    $fasilitas = Fasilitas::find($id);
    if (!$fasilitas) {
        return redirect()->route('fasiliti.index')->with('error', 'Fasilitas tidak ditemukan.');
    }

    // Siapkan data untuk diupdate
    $data = [
        'nama' => $request->input('nama'),
        'keterangan' => $request->input('keterangan'),
        'status' => (bool) $request->input('status'),
    ];

    // Jika ada file gambar baru yang diunggah
    if ($request->hasFile('cover')) {
        $file = $request->file('cover');
        $path = '/img/kempen';
        $fileName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
        $publicId = date('Y-m-d_His') . '_' . $fileName;

        // Unggah ke Cloudinary
        $upload = Cloudinary::upload(
            $file->getRealPath(),
            [
                "public_id" => $publicId,
                "folder" => $path,
            ]
        );

        $data['gambar'] = $upload->getSecurePath();
    }

    // Update data fasilitas
    $fasilitas->update($data);

    return redirect()->route('fasiliti.index')->with('success', 'Fasilitas berhasil diupdate.');
}

    
}
Leave a Comment