简体   繁体   中英

Laravel Storing BIgInt in Session

I'm trying to store some user related information in a Session and it works out just fine when the user doesn't have a bigInteger id, but when they do it simply doesn't work out well.

When I use dd(); in a route located in web.php (GET Request) the id shows up just fine, but when I try to dd(); it out somewhere else it simply returns null.

For example I have a button that says "Link Account" when the user is not logged in and it's supposed to say "Logout" when the session is available.

Also I forward the user into a specific page that is only accessible by logged in users (users that have session data stored) and it enters the page without an issue, but after that redirection the user cannot go to any other page that's restricted to non-logged users.

I'm suspecting that it's related to the integer type, since all the other users do not have a single issue on performing any operations whatsoever.

Any help would be appreciated thank you.

The Procedure more or less looks like this.

Storing the ID in a Controller:

$userId = 221345230; // The number looks like that x)
Session::put('userId', $userId);

Checking the ID in a middleware:

if(!session('userId')){
   return redirect("/");
}

Checking the ID in a view:

@if(!session('userId'))
 <a>Link Account</a>
@else
 <a>Logout</a>

The issue was that I had not changed the types inside the User Model.

Meaning while making a request to get the user data from the database the model was sending an integer and not a string, which will not succeed when you're working with BIGINT in MySQL.

So make sure you change your types everywhere needed guys:D

Example

// Wrong
$user_id = 222234312432;
User::where("user_id", $user_id);

// Correct
$user_id = (string) 222234312432;
User::where("user_id", $user_id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM