Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - log() expects parameter 1 to be float, array given

Tags:

laravel

I am trying to print the content of a json request:

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->json()->all();
        return response()->json($data);
    }

First I tried to print the content to a file with this:

$data = $request->json()->all();
Info($data);

It writes this to the log:

[2019-06-14 10:43:51] local.ERROR: log() expects parameter 1 to be float, array given {"exception":"[object] (ErrorException(code: 0): log() expects parameter 1 to be float, array given at C:\\Users\\User 1\\Coding\\data-service\\app\\Http\\Controllers\\FileController.php:39)
[stacktrace]

Update:

I'm using Log:debug from Jerodev's answer but it still doesn't work.

public function store(Request $request)
{
   $data = $request->json()->all();
   //\Log::info($data);
   Log::debug($data);
   return response()->json($data);
}

Log file:

local.ERROR: Class 'App\Http\Controllers\Log' not found {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Class 'App\\Http\\Controllers\\Log' not found at ...

I've included the class like this:

use App\Http\Controllers\Log;

Without the logging the response to the request is an empty array:

[]

How can I send a response this and log it?

{
  test: 123,
  debug: [...] <- $data here
}

JSON request:

{
    filename: "test1.jpeg"
}

enter image description here

like image 929
matt Avatar asked Nov 18 '25 09:11

matt


1 Answers

You are actually calling the PHP log() function, which calculates the natural logarithm and indeed expects a parameter of type float.

To write log messages in Laravel, you need to use the Log class.

Log::debug($data);
like image 115
Jerodev Avatar answered Nov 21 '25 08:11

Jerodev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!