Profile is a package to store basic information - addresses, phone numbers, emails, and websites using Polymorph approach.
Install Profile package by running in your terminal:
composer require cleaniquecoders/profile
Publish migrations files:
php artisan vendor:publish --tag=profile-migrations
Then run:
php artisan migrate
Then run default seeders:
php artisan profile:seed
Now you are able to configure your own models and type name. See config/profile.php
.
You may want to define your own seeders for profile:seed
in config/profile.php
.
User Cases:
- A company has addresses, phone numbers, emails and websites.
- An employee has addresses, phone numbers, emails and websites.
This lead us to use Polymorph to tackle the issue of similarity in data stored.
Available traits for polymorph:
CleaniqueCoders\Profile\Concerns\Addressable
CleaniqueCoders\Profile\Concerns\Emailable
CleaniqueCoders\Profile\Concerns\Phoneable
CleaniqueCoders\Profile\Concerns\Websiteable
CleaniqueCoders\Profile\Concerns\Bankable
For most common setup for entity is to use HasProfile
trait.
HasProfile
trait consist of:
CleaniqueCoders\Profile\Concerns\Addressable
CleaniqueCoders\Profile\Concerns\Emailable
CleaniqueCoders\Profile\Concerns\Phoneable
CleaniqueCoders\Profile\Concerns\Websiteable
namespace App;
use CleaniqueCoders\Profile\Concerns\HasProfile;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasProfile;
}
Create a record for each profile
$user->addresses()->create([
'primary' => '9 miles, Sungei Way',
'secondary' => 'P.O.Box 6503, Seri Setia',
'city' => 'Petaling Jaya',
'postcode' => '46150',
'state' => 'Selangor',
'country_id' => config('profile.providers.country.model')::name('Malaysia')->first()->id
]);
$user->phones()->create([
'phone_number' => '+6089259167',
'is_default' => true,
'phone_type_id' => PhoneType::HOME,
]);
$user->phones()->create([
'phone_number' => '+60191234567',
'is_default' => true,
'phone_type_id' => PhoneType::MOBILE,
]);
$user->phones()->create([
'phone_number' => '+60380001000',
'is_default' => true,
'phone_type_id' => PhoneType::OFFICE,
]);
$user->phones()->create([
'phone_number' => '+60380001000',
'is_default' => true,
'phone_type_id' => PhoneType::OTHER,
]);
$user->phones()->create([
'phone_number' => '+60380001001',
'is_default' => true,
'phone_type_id' => PhoneType::FAX,
]);
// you can futher query using local scopes in phone models.
// get the first home phone number
$user->phones()->home()->first();
// get all the mobile phone numbers
$user->phones()->mobile()->get();
$user->emails()->create([...]);
$user->websites()->create([...]);
$user->bankable()->create([...]);
Get all records
$user->addresses;
$user->emails;
$user->phones;
$user->websites;
$user->banks;
This package is open-sourced software licensed under the MIT license.