I am getting a encoding error with eloquent and i don't understand how to fix it
this is the the error:
Illuminate\Database\Eloquent\JsonEncodingException: Error encoding model [App\Models\UserIp] with ID [] to JSON: Recursion detected in file
this is the the function :
/**
* @param string $ip the user ipaddress
* @param string $status failed or succeeded ?
* @param string $type the type of action performed
*
* @throws \JsonException
*/
function logUserIp(string $ip, string $status, string $type)
{
$user = UserIp::where('ip', '=', $ip)->where('user_id', '=', auth()->user()->id)->first();
$position = Location::get($ip);
if (null == $user && false !== $position) {
$ip = new UserIp();
$ip->user_id = auth()->user()->id;
$ip->ip = $ip;
$ip->country = $position->countryName;
$ip->country_code = $position->countryCode;
$ip->status = $status;
$ip->type = $type;
$ip->device = json_encode([
'platform' => Agent::platform(),
'browser' => Agent::browser(),
'user_agent' => \request()->server('HTTP_USER_AGENT')
], JSON_THROW_ON_ERROR);
$ip->save();
}
}