PHP code example of andydefer / laravel-addresses
1. Go to this page and download the library: Download andydefer/laravel-addresses library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
andydefer / laravel-addresses example snippets
'providers' => [
// ...
AndyDefer\LaravelAddresses\AddressesServiceProvider::class,
],
use AndyDefer\LaravelAddresses\Services\AddressService;
use AndyDefer\LaravelAddresses\Records\AddressRecord;
use AndyDefer\LaravelAddresses\Enums\AddressType;
use AndyDefer\PhpVo\Enums\Country;
use AndyDefer\PhpVo\ValueObjects\PostalCodeVO;
class UserController extends Controller
{
public function store(AddressService $addressService)
{
$user = User::find(1);
$record = AddressRecord::from([
'street' => '123 Rue Principale',
'city' => 'Paris',
'country' => Country::FR,
'postal_code' => PostalCodeVO::from('75001'),
'address_type' => AddressType::PRIMARY,
]);
$address = $addressService->add($user, $record);
return $address;
}
}
$addresses = $addressService->all($user);
$billingAddresses = $addressService->byType($user, AddressType::BILLING);
$primary = $addressService->primary($user);
$address = $addressService->find($addressId);
$count = $addressService->count($user);
$hasShipping = $addressService->hasType($user, AddressType::SHIPPING);
$updateRecord = AddressRecord::from([
'street' => '456 Nouvelle Rue',
'city' => 'Lyon',
'country' => Country::FR,
'postal_code' => PostalCodeVO::from('69001'),
]);
$updated = $addressService->update($addressId, $updateRecord);
// Définir les métadonnées à NULL
$updated = $addressService->updateRaw($addressId, [
'metadata' => null,
]);
// Définir les coordonnées à NULL
$updated = $addressService->updateRaw($addressId, [
'geo_coordinates' => null,
]);
// Mise à jour partielle
$updated = $addressService->updateRaw($addressId, [
'street' => 'Rue Mise à Jour',
'address_type' => AddressType::BILLING->value,
]);
$deleted = $addressService->delete($addressId);
// Retourne true si supprimé, false sinon
$addressService->setPrimary($user, $addressId);
$primary = $addressService->primary($user);
use AndyDefer\LaravelAddresses\Records\AddressFilterRecord;
use AndyDefer\Repository\Records\FindByRecord;
$filter = new AddressFilterRecord(
addressable_type: 'App\Models\User',
addressable_id: 1,
address_type: AddressType::BILLING,
city: 'Paris',
country: Country::FR,
postal_code: '75001'
);
$findByRecord = new FindByRecord(
filters: $filter,
limit: 10,
offset: 0,
order_by: 'created_at',
order_direction: 'desc'
);
$addresses = $addressRepository->findBy($findByRecord);
$address = Address::find(1);
// Accès sous forme de Value Objects
$postalCode = $address->postalCode; // PostalCodeVO
$coordinates = $address->coordinates; // CoordinatesVO
$metadata = $address->metadata; // StrictDataObject
$createdAt = $address->createdAt; // DateTimeVO
$updatedAt = $address->updatedAt; // DateTimeVO
$deletedAt = $address->deletedAt; // DateTimeVO
bash
php artisan vendor:publish --tag=Addresses-migrations
bash
php artisan migrate