I am totally new to PHP and Laravel Framework.I want to print the variables in Laravel on server side to check what values they contains. How can I do console.log
or print the variable values on the server to see what those contains in Laravel PHP.
Asked
Active
Viewed 1.1k times
8

Laurence
- 58,936
- 21
- 171
- 212

Sajid Ahmad
- 1,124
- 4
- 18
- 40
2 Answers
12
The Shift Exchange's answer will show you the details on the page, but if you wish to log these variables without stopping the processing of the request, you should use the inbuilt Log
class:
You can log at various "levels" and when combined with PHP's print_r()
function inspect all manner of variables and arrays in a human-readable fashion:
// log one variable at "info" logging level:
Log::info("Logging one variable: " . $variable);
// log an array - don't forget to pass 'true' to print_r():
Log::info("Logging an array: " . print_r($array, true));
The .
above between the strings is important, and used to join the strings together into one argument.
By default these will be logged to the file here:
/app/storage/log/laravel.log

msturdy
- 10,479
- 11
- 41
- 52
-
Thanx, It is working. I was thinking it also prints the log or data on terminal as itis done in some other frameworks like Django – Sajid Ahmad May 23 '14 at 06:34
-
@Sajid, great! You always can use Unix commands such as `tail` or `less` to monitor the log file in the terminal.. – msturdy May 23 '14 at 10:37
6
The most helpful debugging tool in Laravel is dd()
. It is a 'print and die' statement
$x = 5;
dd($x);
// gives output "int 5"
What is cool is that you can place as many variables in dd()
as you like before dying:
$x = 5;
$y = 10;
$z = array(4,6,6);
dd($x, $y, $z);
gives output
int 5
int 10
array (size=3)
0 => int 4
1 => int 6
2 => int 6

Laurence
- 58,936
- 21
- 171
- 212
-
2While I agree perfectly with you, isn't OP asking for the [logging](http://laravel.com/docs/errors#logging) commands? – Joel Hinz May 22 '14 at 13:02
-
1I think he is asking for both (he also said 'print the variable'). And since he is "totally new to PHP and Laravel" - I'm just keeping it simple. – Laurence May 22 '14 at 13:06
-
yes, I am totally new to PHP, so want to see whatever i am sending in request from browser. and both the ways are equally accepted – Sajid Ahmad May 23 '14 at 10:53