customising Job and job table in Laravel queue/ rename jobs table - php

When I try php artisan queue:table
It gave me the following error
[InvalidArgumentException]
A CreateJobsTable migration already exists.
It is because I have already the migration named CreateJobsTable for other purpose. I cannot rename this table and migration . Is there any way to rename the migration to CreateJobsQueueTable or some thing relevant?
can we rename the jobs table that artisan creates with 'queue:table'?

Yes. Edit this file config\queue.php:
<?php
return [
....
'connections' => [
....
'database' => [
'driver' => 'database',
'table' => 'jobs', <------ Edit this to something else
'queue' => 'default',
'retry_after' => 90,
],
....
],
....
];
Change the table name to other value, and it should pick up by the TableCommand. Check out Illuminate\Queue\Console\TableCommand on how it uses this value. It's pretty much straightforward :)

Related

Laravel log file specific to a package

I'm writing a couple of laravel packages and I'm wondering if it is possible to have the package write to a specific log file but only for messages related to the package?
I tried making a logging.php file in the packages/myorg/mypackage/config (below) but it doesn't seem to do anything.
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
return [
'default' => env('LOG_CHANNEL', 'stack'),
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/mypackage.log'),
'level' => env('LOG_LEVEL', 'debug'),
]
]
];
I am using "jeroen-g/laravel-packager" to set up the packages. It appears to manually load the mypackage.config in the ServiceProvider bootForConsole
protected function bootForConsole(): void
{
// Publishing the configuration file.
$this->publishes([
mypackage.'/../config/mypackage.php' => config_path('mypackage.php'),
], 'mypackage.config');
}
I'm not sure how to add custom logging to that though. I'm still learning Laravel and I'm not quite sure what or how the main applications config/logging.php is read so I'm not quite sure how to inject a custom version for an add-on package.
EDIT:
I found a post that suggested using the following in the ServiceManager boot() method:
$this->app->make('config')->set('logging.channels.mychannel', [
/* settings */
]);
I used the package config to set a 'logging' => [ 'channels' => [ 'mychannel' => [ /* settings */ ] ] ] and could then do the same thing as above with:
$this->app->make('config')->set('logging.channels.mychannel', config('mypackage.logging.channels.mychannel');
But that still required something in the code. The next best thing I have found thus far is to change my config/logging.php to config/logging.channels.php and include something like:
return [
'mychannel' => [
'driver' => 'single',
'path' => storage_path('logs/mypackage.log'),
'level' => env('LOG_LEVEL', 'debug'),
]
];
Then in the service provider register() method add:
$this->mergeConfigFrom(__DIR__ . '/../config/logging.channels.php', 'logging.channels');
I tried doing it from the original 'logging.php' with channels array nested in a 'logging' key, but array_merge doesn't appear to merge the nested elements so my channel never showed up in logging.channels.
I'm not sure if this is ideal, however. I'd still like to know if there is a 'better' or best practices way of adding custom package logging parameters and whether there is a need to publish it in any way (and how).

How to dynamically pass the variable name into the model in the laravel provider's model config.php file

For instance let's say the
'''
$model_name = 'student_table';
'''
'providers' => [
'users' => [
'$model_name' => [
'driver' => 'eloquent',
'model' => App\Models\.$model_name.::class
]
]
]
The problem is that laravel fires an unexpected '.' expecting identified (T_STRING)
And I need to insert the model name dynamically because there lot of database tables to connect with. Any other solution pls
that's not going to work, providers are loaded way before the controller is.
maybe if you use a config table in database that orient users to own specific auth table.
https://laravel.com/docs/5.7/lifecycle
That is a syntax error.
After App\Models\ you can't have ., It expects you to write the class name next.
Try this:
'providers' => [
'users' => [
'$model_name => [
'driver'=> 'eloquent',
'model' => "App\Models\\" . $model_name
]
]
]
Notice you don't need ::class anymore as you have the full namespace of the class in string.

How to remove the environment variables from Laravel Debug?

I want to remove the environment variables, database details and the information showing on Laravel Debug.
Here is the screenshot:
Go to config/app and add the following
/**
* Debug Blacklist
*/
'debug_blacklist' => [
'_COOKIE' => array_keys($_COOKIE),
'_SERVER' => array_keys($_SERVER),
'_ENV' => array_keys($_ENV),
],
it will replace all environment variables value with an asterisks (*******)
This is from my config/app file:
'debug_blacklist' => [
'_ENV' => [
'APP_KEY',
'DB_DATABASE',
'DB_PASSWORD',
'DB_USERNAME',
'REDIS_PASSWORD',
'MAIL_PASSWORD',
'PUSHER_APP_KEY',
'PUSHER_APP_SECRET',
],
'_SERVER' => [
'APP_KEY',
'DB_DATABASE',
'DB_PASSWORD',
'DB_USERNAME',
'REDIS_PASSWORD',
'MAIL_PASSWORD',
'PUSHER_APP_KEY',
'PUSHER_APP_SECRET',
],
'_POST' => [
'password',
],
],
Add this array to our config/app file, the the system will replace real values with ***.
This way is useful if you want control on specific key items.
You can make your own error pages. (official documentation)
Make a blade file for 500 errors. (resources/views/errors/500.blade.php)
And print error messages or trace log
<h2>{{ $exception->getMessage() }}</h2>
Change from APP_DEBUG=true to APP_DEBUG=false in the .env file. Then you can rely on the laravel logs for your errors. (yourLarvelApp/storage/logs)
Go to your project file: open .env file in notepad and find out APP_DEBUGAPP_DEBUG=true you just rename false; and save. your problem is solve.

How to create a new database by Yii2 migration, when I have to use it by default?

I have a task which is described like "Creating a new database with Yii2 migration, and use it by default". But I have some collusion: I have to set up at lest one default database in my main-local.php file to start migration. But also I have to create this database with initial migration.
return [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=default_base',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
];
How can I deal with it? To create a database with migration and set up as default one.
When I execute my migration (there is code from my migration file):
public function safeUp()
{
$this->execute("CREATE DATABASE default_base");
}
I have an exception:
General error: 1007 Can't create database 'default_base'; database exists
I realize that is because I have already created default_base in phpmyadmin (click button "created database"), and connected it with app in my main-local.php file.
But I need this base to be created only when I execute yii migrate.
UPD
I have tried the way rob006 recommended but I get connection error
Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [1049] Unknown database 'default_base''
My main-local.php looks like the way exactly rob006 recommends, but I still can`t migrate. It works only if I set:
'preinstallDb' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=already_exists_db',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
With the already exists database. What do I do wrong again?
You can define DSN without database name:
'dsn' => 'mysql:host=localhost',
And use this connection to create database.
Yii::$app->preinstallDb->createCommand('CREATE DATABASE default_base')->execute();
However you can't really create database in migration - MigrateController needs database for migration table to save migrations history. You would need to create database before MigrateController will try to access migration table. You can do this by overriding MigrateController::getMigrationHistory():
class MigrateController extends \yii\console\controllers\MigrateController {
protected function getMigrationHistory($limit) {
Yii::$app->preinstallDb->createCommand('CREATE DATABASE IF NOT EXISTS default_base')->execute();
return parent::getMigrationHistory($limit);
}
}
And use your controller in console app config:
'controllerMap' => [
'migrate' => [
'class' => MigrateController::class,
],
],
But honestly, it is ugly solution with a lot of hardcoding, it may be better to create some setup utility which will perform database creation outside of main app. Also usually for security reasons DB user used by application should not even have privileges to create database, so you may reconsider your use case - do you really need to create database by app?
I find the only way make it work for me. Just overriding init() in MigrateController.
First I added controllerMap in main-local.php file:
'controllerMap' => [
'migrate' => [
'class' => 'console\controllers\MigrateController',]]
Then put new controller in console\controllers:
namespace console\controllers;
use yii\console\controllers\MigrateController as BasicMigrateController;
use Yii;
class MigrateController extends BasicMigrateController
{
public function init()
{
Yii::$app->preinstallDb->createCommand('CREATE DATABASE IF NOT EXISTS default_base')->execute();
parent::init();
}
}

Laravel Queue not work

I used laravel 5 and Queue. try this
$job = (new InstallTheme())->onQueue('install_theme')->delay(20);
dispatch($job);
not work
$job = (new InstallTheme())->delay(20);
dispatch($job);
work
Why the first option does not work?
upd
laravel work only if fuild "queue" in table 'jobs' = default
how to fix this?
i think setting queue.php ?
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'retry_after' => 90,
],
As I recall
$job = (new InstallTheme())->onQueue('install_theme')->delay(20);
dispatch($job);
puts the job into the install_theme queue while your other code puts it into the default queue. Please try to run the queue worker with this parameters.
php artisan queue:work --queue=install_theme
This should specifically process the job from this queue.

Categories