Giter Club home page Giter Club logo

profile's Introduction

Profile

Profile is a package to store basic information - addresses, phone numbers, emails, and websites using Polymorph approach.

Latest Stable Version Total Downloads Scrutinizer Code Quality License

Installation

  1. In order to install Profile in your Laravel project:
$ composer require cleaniquecoders/profile
  1. Then in your config/app.php add the following to the providers key:
\CleaniqueCoders\Profile\ProfileServiceProvider::class,

Publish factory files:

$ php artisan vendor:publish --tag=profile-factories

Publish migrations files:

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

Publish seed files:

$ php artisan vendor:publish --tag=profile-seeds

Then run:

$ php artisan migrate

Then seed the Country and Phone Types data with:

$ php artisan profile:seed

Configuration

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.

Available Polymorph Traits

User Cases:

  1. A company has addresses, phone numbers, emails and websites.
  2. An employee has addresses, phone numbers, emails and websites.

This lead us to use Polymorph to tackle the issue of similarity in data stored.

Setup

Available traits for polymorph:

  1. CleaniqueCoders\Profile\Traits\Morphs\Addressable
  2. CleaniqueCoders\Profile\Traits\Morphs\Emailable
  3. CleaniqueCoders\Profile\Traits\Morphs\Phoneable
  4. CleaniqueCoders\Profile\Traits\Morphs\Websiteable
  5. CleaniqueCoders\Profile\Traits\Morphs\Bankable

For most common setup for entity is to use HasProfile trait.

HasProfile trait consist of:

  1. CleaniqueCoders\Profile\Traits\Morphs\Addressable
  2. CleaniqueCoders\Profile\Traits\Morphs\Emailable
  3. CleaniqueCoders\Profile\Traits\Morphs\Phoneable
  4. CleaniqueCoders\Profile\Traits\Morphs\Websiteable
namespace App;

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

class User extends Authenticatable
{
	use HasProfile;
}

Usage

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;

License

This package is open-sourced software licensed under the MIT license.

profile's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar nasrulhazim avatar peter279k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

profile's Issues

Configurable Model and Type

For instance:

<?php

namespace CleaniqueCoders\Profile\Traits\Morphs;

/**
 * Emailable Trait.
 */
trait Emailable
{
    /**
     * Get all emails.
     */
    public function emails()
    {
        return $this->morphMany(config('observers.emailable.model'), config('observers.emailable.type'));
    }
}

in config file:

'emailable' => [
    'model' => \CleaniqueCoders\Profile\Models\Email::class,
    'type' => 'emailable'
]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.