How to print out value on controller Laravel?

How to print out value on controller Laravel?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Print Out Values in Laravel Controllers

You cannot use console.log() directly inside a Laravel controller. That syntax belongs to JavaScript execution environments, and even if you were running a mixed-language project, PHP would throw a fatal error attempting to execute that function call. To debug values or output data for inspection in a PHP application, you need to leverage tools designed for the server-side runtime.

Laravel provides several standard mechanisms for this, ranging from simple logging to specialized debugging helpers. The most common approach for quick checks is using dd() (dump and die) or Log::info(). Each serves a different purpose depending on whether you want to stop execution immediately or let the request continue while recording the data.

Stopping Execution with dd()

The quickest way to inspect a variable's value and halt the response is dd(). This helper stands for "dump and die." It outputs the variable using Laravel's debugging engine and then terminates the script immediately, preventing any subsequent code from running and ensuring no default JSON or view is returned.

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class TestMeController extends Controller
{
    public function __construct()
    {
        // Ensure this middleware is configured if you expect JSON responses only
        $this->middleware('jsonify'); 
    }

    public function callMe($id)
    {
        // Capture request parameters
        $params = Request::all();

        // Output the specific ID and stop execution
        dd($id); 

        // Code below this line will never run
        return response()->json(['message' => 'Success']);
    }
}

When you trigger this endpoint, your browser or terminal will display a formatted dump of the $id variable. This is invaluable for debugging logic errors where an expected value isn't present, but be careful: if you forget to remove dd() before merging code into production, it will break the API by returning a debug screen instead of the intended JSON response.

Logging Without Stopping the Request

If you need to verify a value but must allow the controller to continue executing and return a proper response, use Laravel's logging facade or the Log channel. This is essential for API endpoints where stopping execution would break the client application.

use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;

class TestMeController extends Controller
{
    public function callMe($id)
    {
        $params = Request::all();

        // Log the value to Laravel's default log channel
        Log::info('Incoming ID', ['id' => $id]);

        return response()->json(['id' => $id]);
    }
}

To view these logs, check your storage/logs/laravel.log file in the project root. You can also configure custom channels in config/logging.php to send logs to external services like Sentry or Papertrail if you are managing a production environment with high traffic.

Using VarDumper for Detailed Inspection

For more granular control over how data is displayed, Laravel uses Symfony's VarDumper component. The dd() helper wraps this, but sometimes you just want the dump without stopping execution. You can use dump($variable) to output the variable and let the script proceed naturally.

dump($id);
// Script continues...
return response()->json(['data' => $id]);

This is particularly useful when debugging complex objects or arrays where you want to inspect the structure without interrupting the request lifecycle. However, keep in mind that dumping large datasets can degrade performance significantly if not handled carefully.

Best Practices for Debugging

Always remember that debugging code intended for production should be treated differently from development code. While dd() is excellent for rapid prototyping and local troubleshooting, relying on it in a public-facing API can lead to unintended behavior if the middleware stack expects a clean JSON payload. If you are building an enterprise application where downtime or unexpected headers matter, prefer logging over halting execution.

For more robust debugging strategies and how to manage these logs effectively within your Laravel architecture, you might find additional resources helpful at https://laravelcompany.com.

Our network