I have a problem with laravel 5.1 queues.
I have beanstalkd already set up in my Homestead vm so all i did was to change the queue driver from the default one to beanstalkd in config/queue.php. I've tried the code below and neither one seem to be queued. They all fired synchronously, as soon as i run the code. I didn't even fire the artisan queue:listen command. What am i doing wrong?
Route::get('/', function () {
// return view('welcome');
Queue::push(function($job)
{
Log::info("Dadas");
$job->delete();
});
$input = [
'name' => 'Mario Bašić',
'email' => 'email#me.com',
'comment' => 'Testing queues',
'subject' => 'Email subject'
];
Mail::queue('emails.test', $input, function($message) use ($input)
{
$message->to($input['email'], $input['name']);
$message->subject($input['subject']);
Log::info('sending');
});
});
Make sure you change the driver in the .env file:
QUEUE_DRIVER=beanstalkd
Changing the value in the config/queue.php to:
'default' => env('QUEUE_DRIVER', 'beanstalkd'),
won't work if another value is set for QUEUE_DRIVER in .env.
Related
When running tests in Laravel (php artisan test), I get an error 'duplicate column name: created_at'. This only occurs when i have field additions in the migrations directory. Am i able to ignore specific files when running Laravel tests? Or is there another way to get round this issue?
migrations:
if i just have this table, the tests work fine:
2021_06_18_134444_create_users_tables.php
after creating this migration, the tests fail:
2021_07_08_135544_add_timestamps_to_all_tables.php
a simple test:
use RefreshDatabase;
public function test_redirect_to_home_page_after_login()
{
$user = User::factory()->make([
'name' => 'Test',
'email' => 'test#hotmail.com',
'password' => bcrypt('123456')
]);
$response = $this->post('login', [
'name' => 'Test',
'email' => 'test#hotmail.com',
'password' => '123456'
]);
$response->assertRedirect('/');
$response->assertSessionHasErrors();
}
I need to disable my register route in Laravel 8. Tried
Auth::routes([
'register' => false,
'login' => false,
]);
but the application threw up an error.
RuntimeException
In order to use the Auth::routes() method, please install the laravel/ui package.
If anyone points out what needs to change, will be grateful.
Thanks
Laravel 8 uses fortify authentication. To disable registration from your laravel app, you need to disable it from fortify, which is located on /config/fortify.php
'features' => [
// Features::registration(), // disable here
Features::resetPasswords(),
Features::emailVerification(),
Features::updateProfileInformation(),
Features::updatePasswords(),
Features::twoFactorAuthentication(),
],
At the end of my routes/web.php was the following line:
require __DIR__.'/auth.php';
In routes/auth.php are listed all additional routes for user login/register/logout. Just comment out or remove /register route from there.
In addition, be sure to disable related routes, in routes/web.php :
Route::get('/register', function() {
return redirect('/login');
});
Route::post('/register', function() {
return redirect('/login');
});
I changed according feature tests in tests/Feature/RegistrationTest.php to try to keep work clean so I needed those redirections.
Remove the registration routes from config/auth.php and then create a config/fortify.php (paste the content from: vendor/laravel/fortify/config/fortify.php) which will override the default settings.
Inside config/fortify.php comment out the first element of features array (Features::registration()) then run php artisan optimize to clear config cache and routes cache.
Now all your removed routes should return 404, you can also check if those still exist with php artisan route:list
config/fortify.php:
<?php
use Laravel\Fortify\Features;
return [
'guard' => 'web',
'middleware' => ['web'],
'passwords' => 'users',
'username' => 'email',
'email' => 'email',
'views' => true,
'home' => '/home',
'prefix' => '',
'domain' => null,
'limiters' => [
'login' => null,
],
'features' => [
//Features::registration(),
Features::resetPasswords(),
Features::emailVerification(),
Features::updateProfileInformation(),
Features::updatePasswords(),
Features::twoFactorAuthentication(),
],
];
Just use:
Auth::routes(['register' => false]);
Do not break your head with different versions of packages and Laravel. Because maybe you don't have fortify.php in your config, or using different packages. All routes are in routes/web now. Just go there and force that '/register' sends to login or any other view you want to:
Route::any('/register', function() {
return view('auth.login');
});
That way you maintain out of reach that feature, but close for when you need it.
Remove this code from routes/auth.php
Route::get('/register', [RegisteredUserController::class, 'create'])
->middleware('guest')
->name('register');
Route::post('/register', [RegisteredUserController::class, 'store'])
->middleware('guest');
Just put this in your /routes/web.php:
Route::any('/register', [App\Http\Controllers\HomeController::class, 'index']);
I'm setting up Laravel echo to broadcast events. But whenever I try to broadcast to a channel the channel name gets an automatic prefix: 'laravel_database_'
I've tried switching the return inside the Event to a regular 'Chanel' as following:
public function broadcastOn()
{
return new Channel('public');
}
but when I look into the laravel-echo-server logs I see it is still being broadcasted on: 'laravel_database_public'.
This way I would need to do the following in my JS:
Echo.channel('laravel_database_public').listen('MessageSent', ({message}) => {
console.log(message);
});
But ofcourse, I want to remove the prefix (or figure out why its there). Hopefully someone can clear this up for me. Thanks in advance.
This is configurable in config/database.php (and I believe even removable) under
'redis' => [
'options' => [
'prefix' => // change here.
]
]
The accepted answer does not work with laravel-echo-server.
The solution is rather to let the whole Redis Laravel config untouched and to run version ^1.6.0 of laravel-echo-server with the proper keyPrefix option in laravel-echo-server.json config file:
{
"databaseConfig": {
"redis": {
"keyPrefix": "laravel_database_"
}
}
}
Source
As at Laravel 7, the config/database.php looks like this
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_') . '_database_'),
],
]
So it searches the .env file for your REDIS_PREFIX variable, if there's none, then it generates laravel_database_ or based on whatever you set APP_NAME to in your .env file.
All you have to do is set your REDIS_PREFIX. You can leave it empty so that there is no prefix at all.
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.
I implemented a new factory to generate random data. But I want to have this random data in the format of de_DE. So usually I create a faker object first, but this is not the case in Laravel 5.1 with the new ModelFactory class. How do I localize this then?
$factory->define(App\Models\AED::class, function($faker) {
return [
'owner' => $faker->company,
'street' => $faker->streetAddress,
'latitude' => $faker->latitude,
'longitude' => $faker->longitude
];
});
In order to change the default locale used by Faker, the easiest way is to simply override the FakerGenerator binding with your own concrete implementation:
// AppServiceProvider.php
$this->app->singleton(FakerGenerator::class, function () {
return FakerFactory::create('nl_NL');
});
On top of your AppServiceProvider.php file add the following lines:
use Faker\Generator as FakerGenerator;
use Faker\Factory as FakerFactory;
For example, the above code will mean all Faker instances are created using the nl_NL provider, thus creating Dutch faker data.
Remember: this has to happen after the DatabaseServiceProvider has been executed, so make sure to put your own AppServiceProvider after all of the Laravel ServiceProviders in your config.php array.
Try
$factory->define(App\Models\AED::class, function($faker) {
$faker->locale = "YOUR_LOCALE";
...
});
Add this either at the top of your ModelFactory.php or in your AppServiceProvider::register() method:
$this->app->singleton(\Faker\Generator::class, function () {
return \Faker\Factory::create('de_DE');
});
You must add Providers eg.
$factory->define(Mylead\Models\UserDetails::class, function($faker) {
$faker->addProvider(new Faker\Provider\pl_PL\Person($faker));
$faker->addProvider(new Faker\Provider\pl_PL\Payment($faker));
return [
'name' => $faker->name,
'surname' => $faker->lastname,
'street' => $faker->streetName,
'city' => $faker->city,
'post_code' => $faker->pesel,
'pesel' => $faker->pesel,
'paypal' => $faker->email,
'bank_name' => $faker->bank,
'bank_account' => $faker->bankAccountNumber,
'created_at' => $faker->dateTime
];
});
For now You can't set manualy Faker locale. It should be changed on Laravel Core