Managing Profiles with Ease

When building modern applications, managing profile information like addresses, phone numbers, emails, and websites can quickly become complex.

Fortunately, the Profile package simplifies this task by offering a polymorphic approach to storing essent…


This content originally appeared on DEV Community and was authored by Nasrul Hazim Bin Mohamad

When building modern applications, managing profile information like addresses, phone numbers, emails, and websites can quickly become complex.

Fortunately, the Profile package simplifies this task by offering a polymorphic approach to storing essential data.

This package provides flexibility, allowing you to easily associate multiple types of data with a variety of models—be it companies, employees, or users.

In this post, we'll explore the features, installation process, and usage of the Profile package and how it can help streamline your application’s data management.

What is the Profile Package?

The Profile package is designed to store addresses, phone numbers, emails, and websites efficiently.

Using polymorphic relationships, this package allows multiple entities—such as companies and users—to share the same data structure while maintaining flexibility.

With traits provided for each profile type, the package makes it easy to link relevant data to your models.

Key Features

  1. Polymorphic Traits: Easily associate addresses, emails, phones, and websites with any entity using built-in traits.
  2. Custom Configuration: Configure your models, seeders, and other behaviors in the config/profile.php file.
  3. Seamless Integration: Use the HasProfile trait to quickly implement the common profile fields.
  4. Prebuilt Seeders and Migrations: Get started with pre-built migrations and seeders that can be published with Artisan commands.

Installation

To install the Profile package, follow these steps:

Step 1: Install the Package

Run the following command in your terminal:

composer require cleaniquecoders/profile

Step 2: Publish the Migrations

Next, publish the migration files using:

php artisan vendor:publish --tag=profile-migrations

Step 3: Run Migrations

After publishing, run the migrations to create the necessary tables:

php artisan migrate

Step 4: Seed Default Data

You can optionally run the provided seeders to populate some initial data:

php artisan profile:seed

Configuration

The Profile package offers a flexible configuration file located at config/profile.php. Here, you can customize:

  • Models: Use your own models if needed.
  • Seeders: Define custom seeders for the profile:seed command.

This gives you complete control over how your profiles are structured and managed.

Using Polymorphic Traits

Example Use Cases

  • A company can have multiple addresses, phone numbers, and websites.
  • An employee can have their own email, phone numbers, and personal website.

To implement these scenarios efficiently, the Profile package provides several traits.

Available Polymorphic Traits

  1. Addressable: For managing multiple addresses.
  2. Emailable: For managing emails.
  3. Phoneable: For storing phone numbers.
  4. Websiteable: For websites.
  5. Bankable: For associating bank details.

To quickly integrate these traits, you can use the HasProfile trait, which includes the common traits:

namespace App;

use CleaniqueCoders\Profile\Concerns\HasProfile;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasProfile;
}

Managing Profiles in Your Application

The package makes it simple to create and retrieve data for your models.

Create Records

Addresses:

$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,
]);

Phone Numbers:

$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,
]);

// Query using local scopes
$homePhone = $user->phones()->home()->first();
$mobilePhones = $user->phones()->mobile()->get();

Websites:

$user->websites()->create([
    'name' => 'Cleanique Coders',
    'url' => 'https://cleaniquecoders.com',
    'is_default' => true,
]);

Emails and Bank Accounts:

$user->emails()->create([...]);
$user->bankable()->create([...]);

Retrieving Profile Data

The Profile package allows you to easily retrieve associated data for a user or any other model:

$user->addresses;
$user->emails;
$user->phones;
$user->websites;
$user->banks;

With just a few lines of code, you can access all related profile data.

Conclusion

The Profile package simplifies the management of essential profile data in your Laravel application by leveraging polymorphic relationships. With features like predefined traits, flexible configuration, and built-in seeders, the package saves developers time and effort while keeping the codebase clean and maintainable.

If you’re building an application that requires managing multiple addresses, phone numbers, emails, or websites for different entities, this package is an excellent choice.

Get Started Today!

Install the Profile package today by running:

composer require cleaniquecoders/profile

Visit the GitHub repository or the Packagist page to learn more about this powerful tool for Laravel developers.

Photo by Rock'n Roll Monkey on Unsplash


This content originally appeared on DEV Community and was authored by Nasrul Hazim Bin Mohamad


Print Share Comment Cite Upload Translate Updates
APA

Nasrul Hazim Bin Mohamad | Sciencx (2024-10-24T17:27:12+00:00) Managing Profiles with Ease. Retrieved from https://www.scien.cx/2024/10/24/managing-profiles-with-ease/

MLA
" » Managing Profiles with Ease." Nasrul Hazim Bin Mohamad | Sciencx - Thursday October 24, 2024, https://www.scien.cx/2024/10/24/managing-profiles-with-ease/
HARVARD
Nasrul Hazim Bin Mohamad | Sciencx Thursday October 24, 2024 » Managing Profiles with Ease., viewed ,<https://www.scien.cx/2024/10/24/managing-profiles-with-ease/>
VANCOUVER
Nasrul Hazim Bin Mohamad | Sciencx - » Managing Profiles with Ease. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/24/managing-profiles-with-ease/
CHICAGO
" » Managing Profiles with Ease." Nasrul Hazim Bin Mohamad | Sciencx - Accessed . https://www.scien.cx/2024/10/24/managing-profiles-with-ease/
IEEE
" » Managing Profiles with Ease." Nasrul Hazim Bin Mohamad | Sciencx [Online]. Available: https://www.scien.cx/2024/10/24/managing-profiles-with-ease/. [Accessed: ]
rf:citation
» Managing Profiles with Ease | Nasrul Hazim Bin Mohamad | Sciencx | https://www.scien.cx/2024/10/24/managing-profiles-with-ease/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.