简体   繁体   中英

Laravel request ip returning null

I'm in localhost trying to run a controller with this:

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.

After reading around it might be because I use Illuminate\\Http\\ so I've changed to 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.

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.

And tried Request::ip() without the \\ and returning 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.

So I tried some checks over \\Request::ip() like:

(\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

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\\

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. 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.

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