Comments (4)
Please post the app:setup
code.
Thanks
from laravel-multitenancy.
Thanks for the reply! Here is my stup code:
<?php
namespace App\Console\Commands;
use App\Setting;
use Illuminate\Console\Command;
use DB;
use Illuminate\Support\Arr;
class Setup extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:setup {--f|filter=all : Whether you only want only one type}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Setup application default data.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->allowedFilters = [
'AccountTypes',
'Accounts',
'Permissions',
'Roles',
'PaymentMethods',
'Settings',
];
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$filter = in_array($this->option('filter'), $this->allowedFilters) ? $this->option('filter') : 'all';
$this->info('Setting up application...');
if ($filter === 'all') {
foreach ($this->allowedFilters as $filter) {
$functionName = 'add' . $filter;
$this->$functionName();
}
} else {
$functionName = 'add' . $filter;
$this->$functionName();
}
$this->info('Finished setting up!');
return 0;
}
private function addAccountTypes()
{
$accountTypes = [
['name' => 'Income'],
['name' => 'Expense'],
];
$existingAccountTypes = DB::table('account_types')->groupBy('name')->pluck('name');
$remainingAccountTypes = array_filter($accountTypes, function ($type) use ($existingAccountTypes) {
return !in_array($type['name'], Arr::flatten($existingAccountTypes));
});
DB::table('account_types')->insert($remainingAccountTypes);
}
private function addAccounts()
{
$this->addAccountTypes();
$accounts = [
[
'name' => 'Rent',
'account_type_id' => 1,
'is_general' => 0,
'is_system' => 1
],
[
'name' => 'Deposit',
'account_type_id' => 1,
'is_general' => 0,
'is_system' => 1
],
];
$existingAccounts = DB::table('accounts')->groupBy('name')->pluck('name');
$remainingAccounts = array_filter($accounts, function ($account) use ($existingAccounts) {
return !in_array($account['name'], Arr::flatten($existingAccounts));
});
DB::table('accounts')->insert($remainingAccounts);
}
private function addPermissions()
{
$existingPermissions = DB::table('permissions')->groupBy('name')->pluck('name');
$remainingPermissions = array_filter(self::permissions(), function ($permission) use ($existingPermissions) {
return !in_array($permission['name'], Arr::flatten($existingPermissions));
});
DB::table('permissions')->insert($remainingPermissions);
}
private function addRoles()
{
$existingRoles = DB::table('roles')->groupBy('name')->pluck('name');
$remainingRoles = array_filter(self::roles(), function ($role) use ($existingRoles) {
return !in_array($role['name'], Arr::flatten($existingRoles));
});
DB::table('roles')->insert($remainingRoles);
}
private function addPaymentMethods()
{
$paymentMethods = [
['name' => 'Cash'],
['name' => 'Cheque'],
['name' => 'Credit Card'],
['name' => 'Bank Transfer'],
];
$existingPaymentMethods = DB::table('payment_methods')->groupBy('name')->pluck('name');
$remainingPaymentMethods = array_filter($paymentMethods, function ($paymentMethod) use ($existingPaymentMethods) {
return !in_array($paymentMethod['name'], Arr::flatten($existingPaymentMethods));
});
DB::table('payment_methods')->insert($remainingPaymentMethods);
}
private function addSettings()
{
if (Setting::exists()) {
return false;
}
Setting::create();
}
public static function roles()
{
return [
['name' => 'Admin', 'guard_name' => 'web'],
['name' => 'Tenant', 'guard_name' => 'web'],
];
}
public static function permissions()
{
return [
// Accounts
['name' => 'view all accounts', 'guard_name' => 'web'],
['name' => 'create accounts', 'guard_name' => 'web'],
// Other permissions.....
];
}
}
from laravel-multitenancy.
If you use DB::
it will use your default connection. You have two way to solve:
- changing your default connection to
tenant
instead oflandlord
- Using
DB::connection('tenant')->table('....')->etc..
from laravel-multitenancy.
Thanks! I applied the 2nd option.
from laravel-multitenancy.
Related Issues (20)
- Current tenant not determined when dispatching queued closures HOT 6
- When dispatching a Tenant Aware Job in sync mode from a test, any prior/current DB transaction is cleared HOT 2
- Model ignore tenant when add Observer HOT 2
- Tenant password reset token is being stored on landlord password_reset_tokens table HOT 1
- Prefixing cache doesn't work fine with file-store HOT 3
- Conflict with Rabbitmq package from vyuldashev HOT 1
- Scheduler : Large (longer) jobs, multiple tenants, stops.
- Tenant-Aware Job Scheduling with Horizon HOT 3
- MakeQueueTenantAwareAction seems to break active requests when Queue Connection is sync HOT 4
- base table or view not found with AWS SQS queue for job TenantAware HOT 12
- randomly receive Invalid catalog name: 1046 No database selected (Connection: mysql, SQL: insert into failed_jobs (uuid, connection, queue, payload, exception, failed_at) on queue sqs HOT 5
- Tenant Aware Queue Fails No Database Selected when running from supervisor in a multi tenant environment using AWS SQS FIFO HOT 2
- Update documentation at https://spatie.be to `v3` HOT 3
- in_array(): Argument #2 ($haystack) must be of type array, null given {"exception":"[object] (TypeError(code: 0): in_array(): Argument #2 ($haystack) must be of type array, null given at /spatie/laravel-multitenancy/src/Actions/MakeQueueTenantAwareAction.php:79) HOT 1
- Cache issue with spatie/laravel-permission in TenantAware Command HOT 5
- Issue with the has function on different connections HOT 1
- I want to Connect to mongo DB for some tables but it is giving "Database connection [mongodb] not configured.", exception: "InvalidArgumentException",…} error
- Custom cache driver missing when switching Tenant HOT 2
- Cannot schedule jobs in Laravel 11 HOT 3
- Can't make route not tenant aware
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-multitenancy.