should be $session_data = session()->pull('referrer');
$referrer = User::whereUsername($session_data )->first(); //Fixed
I observed that laravel was having difficulty retrieving the referrerfrom the session and querying the database at the same time which was giving error when getting the id. The solution was to first get the referrer value from the session and pass it to the eloquent.
This is because $referrer does not return a boolean value instead a user object. So using it this way will return an error and of course logical error.
What I did in my case, was to check that referrer value from session was not null.
Should be 'referrer_id' => $session != null ? $referrer->id : null,
or 'referrer_id' => !is_null($session) ? $referrer->id : null,
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Observation One
$referrer = User::whereUsername(session()->pull('referrer'))->first();//Giving issuesshould be
$session_data = session()->pull('referrer');//Fixed$referrer = User::whereUsername($session_data )->first();
I observed that laravel was having difficulty retrieving the
referrerfrom the session and querying the database at the same time which was giving error when getting theid. The solution was to first get the referrer value from the session and pass it to the eloquent.Observation 2
'referrer_id' => $referrer ? $referrer->id : null,
This is because
$referrerdoes not return a boolean value instead a user object. So using it this way will return an error and of course logical error.What I did in my case, was to check that referrer value from session was not null.
Should be
'referrer_id' => $session != null ? $referrer->id : null,
or
'referrer_id' => !is_null($session) ? $referrer->id : null,