49 lines
1.3 KiB
PHP
49 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Pelanggan;
|
|
use App\Models\Kategori;
|
|
use Illuminate\Http\Request;
|
|
|
|
class PelangganController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$query = Pelanggan::query();
|
|
|
|
// Filter kategori
|
|
if ($request->filled('kategori')) {
|
|
$query->where('kategori', $request->kategori);
|
|
}
|
|
|
|
// Pencarian
|
|
if ($request->filled('search')) {
|
|
$search = $request->search;
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('nama_pemesan', 'like', "%$search%")
|
|
->orWhere('email', 'like', "%$search%")
|
|
->orWhere('no_tlpn', 'like', "%$search%");
|
|
});
|
|
}
|
|
|
|
// Pakai pagination
|
|
$pelanggans = $query->orderBy('created_at', 'desc')->paginate(10);
|
|
$kategoris = Kategori::all();
|
|
|
|
return view('admin.pelanggan.index', compact('pelanggans', 'kategoris'));
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$pelanggan = Pelanggan::with('details')->findOrFail($id);
|
|
return view('admin.pelanggan.show', compact('pelanggan'));
|
|
}
|
|
|
|
public function destroy(Pelanggan $pelanggan)
|
|
{
|
|
$pelanggan->delete();
|
|
return redirect()->route('admin.pelanggan.index')->with('success', 'Pelanggan berhasil dihapus!');
|
|
}
|
|
}
|