Installation
composer require sylius/customer
Add the bundle to your config/bundles.php (if using Symfony) or register the service provider in Laravel via config/app.php:
'providers' => [
// ...
Sylius\Customer\CustomerBundle::class,
],
Database Migration Publish and run migrations:
php artisan vendor:publish --provider="Sylius\Customer\CustomerBundle\CustomerBundle" --tag="migrations"
php artisan migrate
First Use Case Create a customer via the repository:
use Sylius\Customer\Model\CustomerRepositoryInterface;
use Sylius\Customer\Model\CustomerInterface;
$customer = new Customer();
$customer->setEmail('john@example.com');
$customer->setFirstName('John');
$customer->setLastName('Doe');
$customerRepository->add($customer);
$customerRepository->flush();
Customer (Entity): Core model for customer data.CustomerRepositoryInterface: For CRUD operations.CustomerEvents: Event system for lifecycle hooks (e.g., CustomerCreatedEvent).Customer Registration
Use the CustomerEvents::CREATED event to trigger post-registration logic (e.g., sending welcome emails):
use Sylius\Component\Customer\Model\CustomerEvents;
event(new CustomerCreatedEvent($customer));
Authentication Integration
Link to Laravel’s Auth system by extending the Customer model:
use Illuminate\Foundation\Auth\User as Authenticatable;
class Customer extends Authenticatable implements CustomerInterface
{
use CustomerTrait;
}
API Endpoints Use Laravel’s API resources to expose customer data:
// routes/api.php
Route::apiResource('customers', CustomerController::class);
Subscriptions/Traits
Extend functionality with traits (e.g., CustomerTrait for common methods):
use Sylius\Component\Customer\Model\CustomerTrait;
class Customer extends Model implements CustomerInterface
{
use CustomerTrait;
}
email, first_name).CustomerEvents in Laravel’s EventServiceProvider:
protected $listen = [
CustomerEvents::CREATED => [
\App\Listeners\SendWelcomeEmail::class,
],
];
createCustomer() helper or factories:
$customer = Customer::factory()->create();
Event Naming Conflicts
Ensure event names (e.g., CustomerCreatedEvent) don’t clash with Laravel’s native events. Prefix if needed:
event(new \App\Events\CustomerRegistered($customer));
Repository Injection
Avoid hardcoding CustomerRepository in controllers. Use dependency injection:
public function __construct(private CustomerRepositoryInterface $customerRepository) {}
Missing Traits
If extending Customer, ensure all required traits (e.g., CustomerTrait) are included to avoid runtime errors.
Database Schema
Published migrations may conflict with existing tables. Review sylius_customer migrations before running:
php artisan vendor:publish --tag="sylius-customer-migrations" --dry-run
public function handle(CustomerCreatedEvent $event)
{
\Log::debug('Customer created:', ['customer' => $event->getCustomer()]);
}
DB_LOG_QUERIES=true
Custom Fields
Add fields via database migrations and update the Customer entity:
$customer->setCustomField('preferred_language', 'en');
Store as JSON or a separate table.
API Filtering Use Laravel’s API resources to filter customer data:
public function toArray($request)
{
return [
'id' => $this->id,
'email' => $this->email,
// Add custom fields here
];
}
Soft Deletes
Enable soft deletes by adding SoftDeletes trait to Customer:
use Illuminate\Database\Eloquent\SoftDeletes;
class Customer extends Model implements CustomerInterface
{
use SoftDeletes, CustomerTrait;
}
Testing Mock the repository in tests:
$this->mock(CustomerRepositoryInterface::class, function ($mock) {
$mock->shouldReceive('find')->andReturn($customer);
});
How can I help you explore Laravel packages today?