14

I'm beginner on Laravel...

How to debug some value on controller in Laravel, result can show to console like syntax console.log() on javascript?

example controller function :

class TestMeController extends Controller {
    public function __construct()
    {
        $this->middleware('jsonify');
    }

    public function callMe($id)
    {
        $params = Request::all();

        console.log($id);  <== how to write code on Laravel?
    }
}

6 Answers 6

24

In Laravel use dd($id) or if you don't want to halt the execution, you can use dump($var). You can still always use PHP's native functions like var_dump, die and print_r.

Sign up to request clarification or add additional context in comments.

Comments

10

You can Use these several methods for printing in Laravel.

1. dump($var)
2. dd($id)
3. var_dump($var)
4. die($var)
5. print_r($var)

Comments

4

dd($var) is great because it stops the program after it runs so by moving it around you get a good sense of which line of code is causing an error.

Comments

0

In my case I am using laravel 7 with blade templates and to test my controller (MVC ARCHITECHTURE) I usually use:

return dd($variable)

or even just

return $variable

Comments

0

If you just want to see the data

$events = new Event();
    
echo "<pre>";
print_r($events->all()->toArray()); 
echo "</pre>";
die;

Comments

-1

Use a return response with json:

return response()->json($dataVar, status_code);

instead console.log();

return response()->json($id, 200);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.