Skip to content

Instantly share code, notes, and snippets.

@Donkfather
Last active September 5, 2018 17:06
Show Gist options
  • Save Donkfather/09b7f886eb8b53da309380581a7695db to your computer and use it in GitHub Desktop.
Save Donkfather/09b7f886eb8b53da309380581a7695db to your computer and use it in GitHub Desktop.
<?php
/*
* The problem
* A user can have many and different types of profiles ( Artist, Manager ...)
* Profiles each have a username that should be unique throughout the platform.
* There should be a way to retrieve a profile by username knowing only the username
*
*
*/
class User extends Model {
public function profiles()
{
return UserProfile::where('user_id', $this->id)->get()
->map(function ($profile) {
$class = Hype::profileClassFor($profile->type);
// return the Profile object ( eg. Artist || Manager )
return $class::whereUsername($profile->username)->first();
});
}
}
class UserProfile extends Model{
protected $with = ['profile'];
public function owner()
{
return $this->belongsTo(User::class);
}
public function profile()
{
return $this->morphTo();
}
}
class Artist extends Model implements Profile {
}
class Manager extends Model implements Profile {
}
Schema::create('users', function(Blueprint $table){
$table->increments('id');
$table->string('email');
});
Schema::create('user_profiles', function(Blueprint $table){
$table->increments('id');
$table->unsignedInteger('user_id');
$table->string('username');
$table->string('type'); // 'artist', 'manager'
});
Schema::create('artists', function(Blueprint $table){
$table->increments('id');
$table->unsignedInteger('user_id');
$table->string('username');
$table->string('name');
//other data
});
Schema::create('managers', function(Blueprint $table){
$table->increments('id');
$table->unsignedInteger('user_id');
$table->string('username');
$table->string('name');
//other different data
});
/*
* There are other components that are applied to profiles
* using the morph relations ( eg. Skill with Skillable )
*
* Usernames should be unique throughout the application
* and i need a way to retrieve the profile by username.
*
* I am aiming to something like this
*
*/
$user->profiles // to return a collection of different profiles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment