You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Customer;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class CustomerController extends Controller
{
public function index(Request $request)
{
$query = Customer::query();
if ($request->q) {
$query->where('name', 'like', "%{$request->q}%");
}
$query->orderBy('created_at', 'desc');
return inertia('Customer/Index', [
'query' => $query->paginate(10),
]);
}
public function store(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'phone' => 'nullable|string|max:255',
'address' => 'nullable|string|max:255',
]);
Customer::create([
'code' => Str::upper(Str::random(6)),
'name' => $request->name,
'phone' => $request->phone,
'address' => $request->address,
]);
return redirect()->route('customer.index')
->with('message', ['type' => 'success', 'message' => 'Item has beed saved']);
}
public function update(Request $request, Customer $customer)
{
$request->validate([
'name' => 'required|string|max:255',
'phone' => 'nullable|string|max:255',
'address' => 'nullable|string|max:255',
]);
$customer->update([
'name' => $request->name,
'phone' => $request->phone,
'address' => $request->address,
]);
return redirect()->route('customer.index')
->with('message', ['type' => 'success', 'message' => 'Item has beed updated']);
}
public function destroy(Customer $customer)
{
$customer->delete();
return redirect()->route('customer.index')
->with('message', ['type' => 'success', 'message' => 'Item has beed deleted']);
}
}