Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to customize model and table name #70

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion config/socialment.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@
],

'models' => [
// If you want to use a custom connected_account model, you can specify it here.
'connected_account' => \ChrisReedIO\Socialment\Models\ConnectedAccount::class,

// If you want to use a custom user model, you can specify it here.
'user' => '\App\Models\User',
'user' => \App\Model\User::class,
],

// The table name for the connected_accounts table
'table_name' => 'connected_accounts',

];
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ return new class extends Migration
{
public function up()
{
Schema::create('connected_accounts', function (Blueprint $table) {
Schema::create(config('socialment.table_name', 'connected_accounts'), function (Blueprint $table) {
$table->id();

$table->foreignId('user_id')->constrained()->cascadeOnDelete();
Expand Down
12 changes: 12 additions & 0 deletions src/Models/ConnectedAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Config;

/**
* @property string $provider
Expand Down Expand Up @@ -32,6 +33,17 @@ class ConnectedAccount extends Model
'expires_at',
];

public function __construct(array $attributes = [])
{
$config = Config::get('socialment');

if (isset($config['table_name'])) {
$this->setTable($config['table_name']);
}

parent::__construct($attributes);
}

public function user(): BelongsTo
{
return $this->belongsTo(config('socialment.models.user'));
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/HasConnectedAccounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ trait HasConnectedAccounts
{
public function connectedAccounts(): HasMany
{
return $this->hasMany(ConnectedAccount::class);
return $this->hasMany(config('socialment.models.connected_account', ConnectedAccount::class));
}
}