后台顾客地址管理
This commit is contained in:
parent
01a032d69e
commit
b055c91c3f
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
/**
|
||||
* AddressController.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author TL <mengwb@opencart.cn>
|
||||
* @created 2022-06-28 20:17:04
|
||||
* @modified 2022-06-28 20:17:04
|
||||
*/
|
||||
|
||||
namespace Beike\Admin\Http\Controllers;
|
||||
|
||||
use Beike\Admin\Http\Resources\CustomerResource;
|
||||
use Beike\Models\Customer;
|
||||
use Beike\Repositories\AddressRepo;
|
||||
use Beike\Repositories\CustomerRepo;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AddressController extends Controller
|
||||
{
|
||||
protected string $defaultRoute = 'addresses.index';
|
||||
public function index(Request $request, int $customerId)
|
||||
{
|
||||
$addresses = AddressRepo::listByCustomer($customerId);
|
||||
$data = [
|
||||
'addresses' => CustomerResource::collection($addresses),
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function store(Request $request, int $customerId)
|
||||
{
|
||||
$data = $request->only(['name', 'phone', 'country_id', 'state_id', 'state', 'city_id', 'city', 'zipcode', 'address_1', 'address_2']);
|
||||
$data['customer_id'] = $customerId;
|
||||
return AddressRepo::create($data);
|
||||
}
|
||||
|
||||
public function update(Request $request, int $customerId, int $addressId)
|
||||
{
|
||||
return AddressRepo::update($addressId, $request->only(['name', 'phone', 'country_id', 'state_id', 'state', 'city_id', 'city', 'zipcode', 'address_1', 'address_2']));
|
||||
}
|
||||
|
||||
public function destroy(Request $request, int $customerId)
|
||||
{
|
||||
CustomerRepo::delete($customerId);
|
||||
|
||||
return ['success' => true];
|
||||
}
|
||||
}
|
||||
|
|
@ -22,7 +22,8 @@ class AddressRepo
|
|||
*/
|
||||
public static function create($data)
|
||||
{
|
||||
return Address::query()->insertGetId($data);
|
||||
$id = Address::query()->insertGetId($data);
|
||||
return self::find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -32,7 +33,12 @@ class AddressRepo
|
|||
*/
|
||||
public static function update($id, $data)
|
||||
{
|
||||
return Address::query()->find($id)->update($data);
|
||||
$address = Address::query()->find($id);
|
||||
if (!$address) {
|
||||
throw new \Exception("地址id {$id} 不存在");
|
||||
}
|
||||
$address->update($data);
|
||||
return $address;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -52,4 +58,14 @@ class AddressRepo
|
|||
{
|
||||
Address::query()->find($id)->delete();
|
||||
}
|
||||
|
||||
public static function listByCustomer($customer)
|
||||
{
|
||||
if (gettype($customer) != 'object') {
|
||||
$customer = CustomerRepo::find($customer);
|
||||
}
|
||||
if ($customer) {
|
||||
return $customer->addresses;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue