GithubHelp home page GithubHelp logo

Comments (4)

masterix21 avatar masterix21 commented on June 22, 2024

Please post the app:setup code.

Thanks

from laravel-multitenancy.

aminraeisi avatar aminraeisi commented on June 22, 2024

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.

masterix21 avatar masterix21 commented on June 22, 2024

If you use DB:: it will use your default connection. You have two way to solve:

  1. changing your default connection to tenant instead of landlord
  2. Using DB::connection('tenant')->table('....')->etc..

from laravel-multitenancy.

aminraeisi avatar aminraeisi commented on June 22, 2024

Thanks! I applied the 2nd option.

from laravel-multitenancy.

Related Issues (20)

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.