Laravel passport install on dynamic database - php

I am creating database for each client registration in my laravel application. I have installed passport for authorization. I have successfully created database and ran migration for passport also. The passport:install command is not working for newly created database. Is there any way to run command passport:install for my new database.
$this->info(sprintf("Dropping database if exists : %s", $dbName));
DBHelper::drop($dbName);
$this->info("Setting up database for client");
//Create migration table
Artisan::call("migrate:install", array(
"--database" => DBHelper::connect($dbName)
));
//Run migration
Artisan::call('migrate',
array('--path' => 'database/migrations/client',
'--database' => DBHelper::connect($dbName))); //DBHelper::connect($dbName) : Create new database config and then DB::reconnect()
//Install passport migration
Artisan::call('migrate', ['--path' => 'vendor/laravel/passport/database/migrations']);
//Install passport
Artisan::call('passport:install');
//Populate database
Artisan::call('db:seed',
array('--database' => DBHelper::connect($dbName)));

After creating database use bellow commands for creating migrations and install passport.
Artisan::call('migrate:refresh', ['--seed' => true]);
Artisan::call('migrate',['--path' => 'vendor/laravel/passport/database/migrations','--force' => true]);
shell_exec('php ../artisan passport:install');

Usually you would use the following code in your controller to excute an Artisan call:
Artisan::call('passport:install');
However, this doesn't work on passport:install and you will get the error:
There are no commands defined in the "passport" namespace
To fix this you must add the following code to boot method at AppServiceProvider.php :
<?php
namespace App\Providers;
use Laravel\Passport\Console\ClientCommand;
use Laravel\Passport\Console\InstallCommand;
use Laravel\Passport\Console\KeysCommand;
use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Schema::defaultStringLength(191);
Passport::routes();
/*ADD THIS LINES*/
$this->commands([
InstallCommand::class,
ClientCommand::class,
KeysCommand::class,
]);
}

Related

Why a merged namespace is created instead of a submodule?

I added my module for creating blanks migration, views, controllers, etc.;
private function createModel()
{
$model = Str::singular(Str::studly(class_basename($this->argument('name'))));
$this->call('make:model',[
'name' => "App\\Modules\\".trim($this->argument('name'))."\\Models\\".$model
]);
}
After running the
php artisan make:module Admin\Users --model --controller
command in the console, I get the wrong structure (pic1)

Twitter API asatymic/twitter with Laravel 8

I followed his readme and I have done composer dump-autoload a million times, but still I receive an error. Don't know what i am making wrong. I am trying to use v2 version of twitter api. Please help me anyone.
In config/app.php:
'providers' => [
...
Atymic\Twitter\ServiceProvider\LaravelServiceProvider::class,
],
'aliases' => [
...
'Twitter' => Atymic\Twitter\Facade\Twitter::class,
],
In my controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Twitter;
class HomeController extends Controller {
public function index() {
$tweets = Twitter::getUserTimeline([
'screen_name' => 'xxxxxxx',
'count' => 10,
'format' => 'json'
]);
dd($tweets);
return view('home');
}
public function about() {
return view('about');
}
}
But I am getting this error:
Error
Call to undefined method Atymic\Twitter\Service\Accessor::getUserTimeline()
Look like you have to import facade
Instead of this
use Twitter;
Import this
use Atymic\Twitter\Facade\Twitter;
If you are using alias then run following command
php artisan config:clear
composer dump-autoload
php artisan optimize
Please look to the twitter api version which you have defined in the .env. you may need to use 1.1 and you are using v2
I think getUserTimeLine is available for version 1.1, and I think as the readme says you can use userTweets for v2

Why does php migration keeps on creating columns on default cashier model (User) even changed to different model

I would like to change the default cashier's model to my custom model (Users to Companies)
What i have done is
changed the services.php's model to App\Models\Companies\Companies::class
changed my .env's to CASHIER_MODEL=App\Models\Companies\Companies
published the cashier migration and alter the table name to companies but
php artisan migrate will still update cashier's default model that is Users
// services.php
'stripe' => [
'model' => App\Models\Companies\Companies::class,
'key' => env('STRIPE_KEY'),
'secret' => env('STRIPE_SECRET'),
'webhook' => [
'secret' => env('STRIPE_WEBHOOK_SECRET'),
'tolerance' => env('STRIPE_WEBHOOK_TOLERANCE', 300),
],
],
CASHIER_MODEL=App\Models\Companies\Companies // on .env
// on migration
public function up()
{
Schema::table('companies', function (Blueprint $table) {
$table->string('stripe_id')->nullable()->collation('utf8mb4_bin')->index();
$table->string('card_brand')->nullable();
$table->string('card_last_four', 4)->nullable();
$table->timestamp('trial_ends_at')->nullable();
});
}
Laravel keeps loading the Cashier migrations from the vendor folder even if you have published them to the database/migrations folder and edited them.
The solution is to explicitly tell Laravel to ignore the vendor migrations.
Add the following to the register method of the AppServiceProvider:
// Use the Cashier migrations from migration folder instead of vendor folder
Cashier::ignoreMigrations();
Remember to also include:
use Laravel\Cashier\Cashier;
Honestly, I would consider this a Laravel Cashier bug. The default Laravel Cashier migrations should create the subscriptions table joining the to Cashier model you define in the Cashier config and it should only add the Cashier columns to that Model. At any rate, here's the full workaround.
Disable the default Laravel Cashier Migrations in AppServiceProvider.php by adding Cashier::ignoreMigrations(); to the register() method.
namespace App\Providers;
use Laravel\Cashier\Cashier;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
Cashier::ignoreMigrations();
}
}
Create the following migrations: php artisan make:migration create_subscriptions_table and php artisan make:migration create_customer_columns
Copy the migration content from the migrations in vendor/laravel/cashier/database/migrations/ into your newly created migration files.
Replace any reference to users and user_id with the Laravel Cashier model you have decided to use.

Lumen - Mono Log - Create Custom log file [duplicate]

I want to add to my Lumen project a daily Log.
I try this in the app.php (Folder Bootstrap/)
$logFile = 'laravel.log';
Log::useDailyFiles(storage_path().'/logs/'.$logFile);
But this set me that error
Call to undefined method Monolog\logger::useDailyFiles()
Any help I appreciate...Thanks
If you look at the framework source code here you can see that it will not do daily logs, but rather write to a single log file lumen.log. There is a public method available configureMonologUsing seen here and referenced here that you can use to override the default behavior without extending the Application.
Lumen just sets a handler to monolog, so another good solution is you could do this:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
class LogServiceProvider extends ServiceProvider
{
/**
* Configure logging on boot.
*
* #return void
*/
public function boot()
{
$maxFiles = 5;
$handlers[] = (new RotatingFileHandler(storage_path("logs/lumen.log"), $maxFiles))
->setFormatter(new LineFormatter(null, null, true, true));
$this->app['log']->setHandlers($handlers);
}
/**
* Register the log service.
*
* #return void
*/
public function register()
{
// Log binding already registered in vendor/laravel/lumen-framework/src/Application.php.
}
}
Then don't forget to add the service provider to your Lumen bootstrap/app.php:
$app->register(\App\Providers\LogServiceProvider::class);
In Lumen 5.6 better way is to configure your default setting in .env as LOG_CHANNEL=daily
By default the setting is LOG_CHANNEL=stack which use single file for logging.
Starting from version 5.6, configuring the logging system is much easier:
Create directory config in your project if it doesn't exist
Copy the logging config file from
vendor/laravel/lumen-framework/config/logging.php to your project config dir
Edit file config/logging.php and adjust the channels property to your liking.
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['daily'],
],

run artisan command from php function

I want to run php artisan passport:client --password from function.
I tryed Artisan::call('passport:client'); and Artisan::command('passport:client'); but it return undefined command
Note: I have alredy install laravel passport and the command is working fine from the terminal
I have found it, in boot() method of the PassportServiceProvider there is a check that essentially prevent it being called from Artisan::call.
//PassportServiceProvider.php at line 37:
if ($this->app->runningInConsole()) {
$this->commands([
Console\InstallCommand::class,
Console\ClientCommand::class,
Console\KeysCommand::class,
]);
...
}
to make it work with general artisan command, we can register these commands ourselves. somewhere within the boot method of AuthServiceProvider maybe.
public function boot() {
$this->commands([
Console\InstallCommand::class,
Console\ClientCommand::class,
Console\KeysCommand::class,
]);
}
Now we can call Artisan::call('passport:install') or the other 2 commands.
From Laravel Docs
Route::get('/foo', function () {
$exitCode = Artisan::call('email:send', [
'user' => 1, '--queue' => 'default'
]);
//
});
Import this line on top.
use Illuminate\Support\Facades\Artisan;
And Enter line below for command
Artisan::call('passport:client --password')

Categories