Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from osinitiative/develop
Browse files Browse the repository at this point in the history
Added Morph for Address, Phone and Email.
  • Loading branch information
nasrulhazim authored Dec 24, 2017
2 parents 150736b + b78edcd commit b08cdd0
Show file tree
Hide file tree
Showing 27 changed files with 701 additions and 19 deletions.
13 changes: 6 additions & 7 deletions app/Macros/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace OSI\Macros\Database\Schema;

use OSI\Contracts\MacroContract;
use Illuminate\Database\Schema\Blueprint as DefaultBlueprint;
use OSI\Contracts\MacroContract;

/**
* Extended Blueprint by using Macro
Expand All @@ -23,11 +23,10 @@ public static function registerMacros()
->nullable();
});

DefaultBlueprint::macro('fkDeleteCascade', function ($key, $table) {
$this->foreign($key)
->references('id')
->on($table)
->onDelete('cascade');
DefaultBlueprint::macro('referenceOn', function ($key, $table, $references = 'id') {
return $this->foreign($key)
->references($references)
->on($table);
});

DefaultBlueprint::macro('addAcceptance', function ($value) {
Expand Down Expand Up @@ -57,7 +56,7 @@ public static function registerMacros()

DefaultBlueprint::macro('user', function () {
$this->addForeign('user_id', 'users');
$this->fkDeleteCascade('user_id', 'users');
$this->referenceOn('user_id', 'users');
});

DefaultBlueprint::macro('amount', function ($label = 'amount') {
Expand Down
27 changes: 27 additions & 0 deletions app/Models/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace OSI\Models;

use Illuminate\Database\Eloquent\Model;

class Address extends Model
{
protected $guarded = ['id'];

/**
* Get all of the owning addressable models.
*/
public function addressable()
{
return $this->morphTo();
}

/**
* Get Country
* @return \OSI\Models\Country
*/
public function country()
{
return $this->belongsTo(Country::class);
}
}
10 changes: 10 additions & 0 deletions app/Models/Country.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace OSI\Models;

use Illuminate\Database\Eloquent\Model;

class Country extends Model
{
protected $guarded = [];
}
18 changes: 18 additions & 0 deletions app/Models/Email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace OSI\Models;

use Illuminate\Database\Eloquent\Model;

class Email extends Model
{
protected $guarded = ['id'];

/**
* Get all of the owning emailable models.
*/
public function emailable()
{
return $this->morphTo();
}
}
18 changes: 18 additions & 0 deletions app/Models/Phone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace OSI\Models;

use Illuminate\Database\Eloquent\Model;

class Phone extends Model
{
protected $guarded = ['id'];

/**
* Get all of the owning phoneable models.
*/
public function phoneable()
{
return $this->morphTo();
}
}
13 changes: 13 additions & 0 deletions app/Models/PhoneType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace OSI\Models;

use Illuminate\Database\Eloquent\Model;

class PhoneType extends Model
{
const HOME = 1;
const MOBILE = 2;
const OFFICE = 3;
const OTHER = 4;
}
9 changes: 8 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use OSI\Traits\HasMediaExtended;
use OSI\Traits\HasProfile;
use OSI\Traits\HasSlugExtended;
use OSI\Traits\HasThumbnail;
use OSI\Traits\LogsActivityExtended;
Expand All @@ -14,7 +15,13 @@

class User extends Authenticatable implements HasMediaConversions
{
use HasMediaExtended, HasThumbnail, HasRoles, HasSlugExtended, LogsActivityExtended, Notifiable, SoftDeletes;
use HasProfile, HasMediaExtended, HasThumbnail, HasRoles, HasSlugExtended, LogsActivityExtended, Notifiable, SoftDeletes;

/**
* Guarded Field
* @var array
*/
protected $guarded = ['id'];

/**
* Create Slug From
Expand Down
4 changes: 2 additions & 2 deletions app/Observers/HashidsObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
class HashidsObserver
{
/**
* Listen to the created event.
* Listen to the creating event.
*
* @param DummyModelClass $DummyModelVariable
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function creating(Model $model)
Expand Down
6 changes: 3 additions & 3 deletions app/Observers/ReferenceObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
class ReferenceObserver
{
/**
* Listen to the created event.
* Listen to the creating event.
*
* @param Model $DummyModelVariable
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function created(Model $model)
public function creating(Model $model)
{
if (Schema::hasColumn($model->getTable(), 'reference') && is_null($model->reference)) {

Expand Down
15 changes: 15 additions & 0 deletions app/Traits/HasProfile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace OSI\Traits;

use OSI\Traits\Morphs\Addressable;
use OSI\Traits\Morphs\Emailable;
use OSI\Traits\Morphs\Phoneable;

/**
* HasProfile Trait
*/
trait HasProfile
{
use Addressable, Emailable, Phoneable;
}
17 changes: 17 additions & 0 deletions app/Traits/Morphs/Addressable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace OSI\Traits\Morphs;

/**
* Addressable Trait
*/
trait Addressable
{
/**
* Get all of the addresses
*/
public function addresses()
{
return $this->morphMany(\OSI\Models\Address::class, 'addressable');
}
}
17 changes: 17 additions & 0 deletions app/Traits/Morphs/Emailable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace OSI\Traits\Morphs;

/**
* Emailable Trait
*/
trait Emailable
{
/**
* Get all of the emails.
*/
public function emails()
{
return $this->morphMany(\CLNQCDRS\Models\Email::class, 'emailable');
}
}
17 changes: 17 additions & 0 deletions app/Traits/Morphs/Phoneable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace OSI\Traits\Morphs;

/**
* Phoneable Trait
*/
trait Phoneable
{
/**
* Get all of the phones.
*/
public function phones()
{
return $this->morphMany(\OSI\Models\Phone::class, 'phoneable');
}
}
2 changes: 1 addition & 1 deletion config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
'model' => OSI\Models\User::class,
],

// 'users' => [
Expand Down
14 changes: 14 additions & 0 deletions database/factories/AddressFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use Faker\Generator as Faker;

$factory->define(\OSI\Models\Address::class, function (Faker $faker) {
return [
'country_id' => $faker->randomElement(range(1, 200)),
'primary' => $faker->streetName,
'secondary' => $faker->streetAddress,
'postcode' => $faker->postcode,
'city' => $faker->city,
'state' => $faker->state,
];
});
9 changes: 9 additions & 0 deletions database/factories/EmailFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Faker\Generator as Faker;

$factory->define(\OSI\Models\Email::class, function (Faker $faker) {
return [
'email' => $faker->companyEmail,
];
});
10 changes: 10 additions & 0 deletions database/factories/PhoneFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Faker\Generator as Faker;

$factory->define(\OSI\Models\Phone::class, function (Faker $faker) {
return [
'phone_type_id' => $faker->randomElement([1, 2, 3, 4]),
'phone_number' => $faker->phoneNumber,
];
});
10 changes: 5 additions & 5 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/
*/

$factory->define(OSI\User::class, function (Faker $faker) {
$factory->define(\OSI\Models\User::class, function (Faker $faker) {
static $password;

return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
];
});
33 changes: 33 additions & 0 deletions database/migrations/2017_12_23_000005_create_countries_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCountriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('countries', function (Blueprint $table) {
$table->increments('id');
$table->string('code');
$table->label();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('countries');
}
}
32 changes: 32 additions & 0 deletions database/migrations/2017_12_23_000009_create_phone_types_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePhoneTypesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('phone_types', function (Blueprint $table) {
$table->increments('id');
$table->label();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('phone_types');
}
}
Loading

0 comments on commit b08cdd0

Please sign in to comment.