File: /var/www/html/dashboard.orbiwheels.com/app/Http/Controllers/Admin/CustomerController.php
<?php
namespace App\Http\Controllers\Admin;
use App\DataTables\Admin\CustomerDataTable;
use App\Http\Controllers\Controller;
use App\Models\Booking;
use App\Models\Customer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class CustomerController extends Controller
{
public function index(CustomerDataTable $dataTable)
{
return $dataTable->render('admin.customer.index');
}
public function create()
{
return view('admin.customer.create');
}
public function store(Request $request)
{
$validatedData = $request->validate([
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|email|unique:customers,email',
'phone' => 'required|string|max:20',
'date_of_birth' => 'nullable|date',
'gender' => 'required|in:male,female,other',
'alternative_phone' => 'nullable|string|max:20',
]);
Customer::create($validatedData);
return redirect()->route('admin.customer.index')
->with('success', 'Customer created successfully!');
}
public function view(string $id)
{
$customer = Customer::findOrFail($id);
// bookings ko paginate karo
$bookings = Booking::with('rides')
->where('customer_id', $customer->id)
->orderBy('id', 'desc')
->paginate(10);
return view('admin.customer.view', compact('customer', 'bookings'));
}
public function edit(string $id)
{
$customer = Customer::find($id);
if (!isset($customer)) {
return redirect()->route('admin.customer.index')->with('error', 'Customer not found!');
}
return view('admin.customer.edit', compact('customer'));
}
public function update(Request $request, string $id)
{
$customer = Customer::find($id);
if (!$customer) {
return redirect()->route('admin.customer.index')->with('error', 'Customer not found!');
}
// dd($request->all());
// Validate request data
$validatedData = $request->validate([
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|email|unique:customers,email,' . $customer->id,
'phone' => 'required|string|max:20',
'date_of_birth' => 'nullable|date',
'gender' => 'required|in:male,female,other',
'alternative_phone' => 'nullable|string|max:20',
]);
// Update customer with validated data
$customer->update($validatedData);
return redirect()->route('admin.customer.index')->with('success', 'Customer updated successfully!');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
$customer = Customer::find($id);
if (!isset($customer)) {
return response()->json([
'status' => false,
'message' => 'Customer not found!',
]);
}
$customer->delete();
return response()->json([
'status' => true,
'message' => 'Customer deleted successfully!',
]);
}
}