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

116 lines
3.1 KiB

<?php
namespace App\Http\Controllers\Partners;
use Illuminate\Http\Request;
use App\Entities\Partners\Customer;
use App\Http\Controllers\Controller;
use App\Http\Requests\Partners\CustomerCreateRequest;
use App\Http\Requests\Partners\CustomerUpdateRequest;
class CustomersController extends Controller
{
/**
* Display a listing of the customer.
*
* @return \Illuminate\View\View
*/
public function index()
{
$customers = Customer::where(function ($query) {
$query->where('name', 'like', '%'.request('q').'%');
})
->latest()
->withCount('projects')
->paginate(25);
return view('customers.index', compact('customers'));
}
/**
* Show the create customer form.
*
* @return \Illuminate\View\View
*/
public function create()
{
return view('customers.create');
}
/**
* Store a newly created customer in storage.
*
* @param \App\Http\Requests\Partners\CustomerCreateRequest $customerCreateForm
* @return \Illuminate\Http\RedirectResponse
*/
public function store(CustomerCreateRequest $customerCreateForm)
{
Customer::create($customerCreateForm->validated());
flash(__('customer.created'), 'success');
return redirect()->route('customers.index');
}
/**
* Show the specified customer.
*
* @param \App\Entities\Partners\Customer $customer
* @return \Illuminate\View\View
*/
public function show(Customer $customer)
{
return view('customers.show', compact('customer'));
}
/**
* Show the edit customer form.
*
* @param \App\Entities\Partners\Customer $customer
* @return \Illuminate\View\View
*/
public function edit(Customer $customer)
{
return view('customers.edit', compact('customer'));
}
/**
* Update the specified customer in storage.
*
* @param \App\Http\Requests\Partners\CustomerUpdateRequest $customerUpdateForm
* @param \App\Entities\Partners\Customer $customer
* @return \Illuminate\Http\RedirectResponse
*/
public function update(CustomerUpdateRequest $customerUpdateForm, Customer $customer)
{
$customer->update($customerUpdateForm->validated());
flash(__('customer.updated'), 'success');
return redirect()->route('customers.show', $customer->id);
}
/**
* Remove the specified customer from storage.
*
* @param \App\Entities\Partners\Customer $customer
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(Customer $customer)
{
// TODO: user cannot delete customer that has been used in other table
request()->validate([
'customer_id' => 'required',
]);
$routeParam = request()->only('page', 'q');
if (request('customer_id') == $customer->id && $customer->delete()) {
flash(__('customer.deleted'), 'warning');
return redirect()->route('customers.index', $routeParam);
}
flash(__('customer.undeleted'), 'danger');
return back();
}
}