John Davidson

php - Laravel 8 - Getting randomly "No application encryption key has been specified"

0 comments
Message:


Please try to read & answer to the question asked, instead of suggesting improvements to public-routing which isn't there by an accident.




Edit #5 (updated @ 20.10.2021) Unfortunately the question remains unanswered. I'm really baffled by this, but even more so, this error hasn't been showing up for past few days, and I have no idea what happened.


I used to have custom static "User::hasRole(['...']"-method which fetched users userRoles everytime roles were checked. Biggest change I made was caching user-roles to session, so that there wouldn't be need to fetch 'em every time (as dozens of rest-queries were referring to checking roles from DB over and over again as part of querybuilders filters).


$userRoles = Session()->get('userRoles', function() {
$user = User::with(['userRoles'])->find(Auth::user()->id);
Session()->put('userRoles', $user->userRoles);
return $user->userRoles;
});

I know this solution could cause an issue at certain points, but due to the nature of my project it's almost nonexistent risk.


These random errors never seemed to point to one specific method and nothing pointed this direction. It looks like this error will remain somewhat a mystery, but at the time being it's not causing issues anymore. Best I can guess is that there were too many parallel PHP/SQL-threads, which somehow lead to kernel pipeline failing at random states when authentication somehow failed.


Thanks everyone who tried to help.


Edit #4 (updated @ 14.9.2021) Changing cache driver from file to array didn't resolve issue. Single endpoint just returned "unauthenticated"-message, even though user is authenticated.


Edit #3, Problem persists, trying to sum up:



  • Key HAS BEEN generated, even regenerated multiple times

  • Laravel 8, XAMPP

  • Have run config:cache multiple times. If config-cache has been cleared the problem happens more often, but currently maybe 1/100-200 REST calls end up failing more or less randomly. This doesn't happen on LAMP production-server.

  • This problem causes randomly different errors, all somehow related to user session

  • No application encryption key

  • 401 - unauthenticated

  • SQL Connection error (even though either refreshing page / rerunning post works like a charm, and only 1 of ~20 REST-actions on single page load might end up to this state).

  • Doesn't seem to be CSRF-token related error since sessions are live (as other REST-calls are not failing), but for some reason Axios doesn't report XSRF-token on RESPONSE-headers whenever this error occurs. Token is present on REQUEST-headers... still looks like backend is unable to find session?

  • Currently using file-based session handling, going to try memory-based


--


I'v been having an issue with Laravel producing HTTP code 500 - "No application encryption key has been specified" -error randomly on my rest-routes. Of course I have set, and even reset the key with Artisan, so that shouldn't be the issue.


This issue has always been present, ever since I started my project on Laravel 6. Quite quickly I was able to track issue to cache. Basically when cache was wiped with "php artisan cache:clear" REST-routes started to fail, about 5% of all the requests. When cache was generated again with "php artisan config:cache" the issue was resolved. For some reason it seems that .env -variables are not registered or read correctly when cache is wiped? Still, wasn't an issue when developing and cache was configured.


I'v upgraded my project from Laravel 6 to 8 and now it seems that this issue has started to reproduce and I'm unable to find the reason.


My application has ~10-20 initial atomic REST-endpoint accesses for providing basic information for Vue SPA initialization. All of these requests happen asynchronously and every-now-and-then (maybe once per 10 page refreshes / reinitializations) one of those calls might fail.


Running things over latest XAMPP. Not using XDebug or anything else which might block calls. Wondering if anyone else has stumbled to this problem?


EDIT:


Additionally I might get very random "401 - unauthenticated" -errors. The problem is very similar to application key -error. For an example this picture was taken after ~20 minutes activity on web-browser while jumping from one page to another. All the other calls before and after were behind authentication as well, but this single 401 came out of nowhere.



Edit 2:


Changing lottery-number didn't fix.



My Answer:
To get random records in Laravel 8, you can use the `inRandomOrder()` method provided by Eloquent. Here's an example of how you can get random records from a database table:

php
use App\Models\Post;

$randomPosts = Post::inRandomOrder()->take(5)->get();

foreach ($randomPosts as $post) {
echo $post->title . "
";
}


In this example, we are getting 5 random posts from the `posts` table and then looping through each post to display its title. You can adjust the `take()` method to get a different number of random records.

Make sure to replace `Post` with your actual model class and `posts` with the name of your database table.

Rate this post

5 of 5 based on 1137 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us