简体   繁体   English

Laravel请求ip返回null

[英]Laravel request ip returning null

I'm in localhost trying to run a controller with this: 我在localhost中尝试与此运行控制器:

use Illuminate\Http\Request;
$transaccion = new Transaction();
$transaccion->user_id = $user_id;
$transaccion->event_id = $event_id;
$transaction->ip = \Request::ip();
$save = $transaccion->save();

But that is returning me this error: Creating default object from empty value on the ip line. 但这使我返回此错误: Creating default object from empty value ip行上的Creating default object from empty value

After reading around it might be because I use Illuminate\\Http\\ so I've changed to use Request; 阅读之后,可能是因为我使用Illuminate \\ Http \\,所以我改为use Request; but the controller is big and is returning this: Undefined property: Illuminate\\Support\\Facades\\Request::$user_id and I wouldn't like to change the whole controller. 但是控制器很大,返回的是: Undefined property: Illuminate\\Support\\Facades\\Request::$user_id ,我不想更改整个控制器。

I've also tried $request->ip but I think it's looking for some input and there's not so I get an error, I think it was undefined ip. 我也尝试了$request->ip但是我认为它正在寻找一些输入,但是没有,所以我得到一个错误,我认为这是未定义的ip。

And tried Request::ip() without the \\ and returning Request::ip() should not be called statically 并试图Request::ip()没有\\和恢复Request::ip() should not be called statically

I think the main "problem" is that I'm in localhost so I don't have IP, but still, I'd like it to work on local. 我认为主要的“问题”是我在本地主机中,所以我没有IP,但是我仍然希望它可以在本地工作。

So I tried some checks over \\Request::ip() like: 所以我尝试对\\Request::ip()一些检查,例如:

(\Request::ip()) ? \Request::ip() : 0;

or 要么

(is_null(\Request::ip()) ? 0 : (\Request::ip());

And some other but weirdly in all of them I'm getting the same Creating default object from empty value 还有其他一些但奇怪的是我都Creating default object from empty value

Any idea of what I'm doing wrong? 有什么想法我做错了吗? Thx 谢谢

But that is returning me this error: Creating default object from empty value 但是,这返回了我这个错误:从空值创建默认对象

After reading around it might be because I use Illuminate\\Http\\ 看完之后可能是因为我使用了Illuminate \\ Http \\

It's not. 不是。 The error message is telling you exactly what's wrong: 错误消息告诉您确切的问题所在:

Creating default object from empty value 从空值创建默认对象

The issue is with the $transaction->ip line. 问题出在$transaction->ip行上。 It appears you're trying to set a property on a $transaction variable before it exists, so PHP assumes you want an object, creates one for you, and assigns it to the $transaction variable. 似乎您正在尝试在$transaction变量上设置属性,因此PHP假定您需要一个对象,为您创建一个对象,并将其分配给$transaction变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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