1

so I am Laravel newbie. Basically, what I am trying to do is to view the "hello world" message when loading the Laravel default landing page. I have the following,

Route::get('/', function () {
    echo"hello world";
    return view('welcome');
});

But "hello world" is not showing up in the terminal. Is that even the proper use of echo in Laravel? Help will be appreciated.

Thanks

3
  • Does this answer your question? How do I write to the console from a Laravel Controller? Commented Oct 11, 2021 at 15:56
  • I changed echo to Log::info('This is some useful information.');, still no output Commented Oct 11, 2021 at 16:01
  • You can just return 'Hello World'; no need echo Commented Oct 11, 2021 at 16:29

1 Answer 1

2

Echo doesn't work like that. You want to show hello world text in your welcome file. Here are some things you might want to know. First, assign hello world string to a variable and pass it to the view like this.

Route::get('/', function () {
  $text = "Hello world!";
  return view('welcome', compact('text'));
});

And in your view file, pass this variable to some HTML tag like this.

<h1>{{ $text }}</h1>

Log is something else, it is for logging the output to a .log file which we can find in /storage/log. Also if you remove the return view statement from the function and instead of this return a direct hello world text it will show up in your browser screen.

Route::get('/', function(){
   return 'Hello World';
});

Hope this answers your question.

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

3 Comments

is there a way I can see the output in terminal? I come from node.js/django background where you can send output message that can be shown in terminal using console.log()andprint()
Yes you can, but that's a very different case with laravel console commands. Sadly laravel lacks what you're asking for.
Console Commands here's the reference if you are interested in exploring it.

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.