How to Write to the Console from a Laravel Controller

How do I write to the console from a Laravel Controller?

Aha!

This can be done with the following PHP function:

error_log('Some message here.');

Found the answer here: Print something in PHP built-in web server

How to print messages on console in Laravel?

Try with

error_log('message here.');

Read More


If You ant add LOG

Log::info('message');

If LOG with an array

Log::info(json_encode($array));

Import Illuminate\Support\Facades\Log;

Output data on the console from Laravel Controller

Finally, I found a solution.

I'm using AWS and some logs are in /var/log/nginx.

I entered the folder and there's many file as prefix "error_log", one for each application.

So, I did:
sudo tail -f error_log_myapplication.log to open console listener.

Then, I wrote error_log('my message') in Controller and call my request.

That's it!

How to print out value on controller Laravel?

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.

Console data in PHP laravel controller

dd is short for dump and die, and will stop execution of your code.

You're probably looking for var_dump() if you're running your code in console, or error_log() if you're using your site in a browser, and watching the logs (with something like tail -f /var/log/apache2/error.log).

how to print variable from laravel controller?

For your purposes, the dd method would definitely be the best, so something like this should work:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class placeId extends Controller
{
public function index(Request $request){
$values = $request->data;
//Print the values
dd($values);
}

To see the result of this print, open up your Browser's console and go to the Networking section. Check the packet related to your request. The dd content should be printed into the preview for your request packet.

How to echo to console in Laravel and Artisan?

Don't know if you are using Laravel 3 or Laravel 4, and if its also possible in Laravel 3, but i found this in the docs.

$this->info('Creating sample users...');

EDIT

If you switch to database seeds you can use this to display a message

$this->command->info('Creating sample users...');

Call laravel controller via command line

There is no way so far (not sure if there will ever be). However you can create your own Artisan Command that can do that. Create a command CallRoute using this:

php artisan make:console CallRoute

For Laravel 5.3 or greater you need to use make:command instead:

php artisan make:command CallRoute

This will generate a command class in app/Console/Commands/CallRoute.php. The contents of that class should look like this:

<?php namespace App\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Illuminate\Http\Request;

class CallRoute extends Command {

protected $name = 'route:call';
protected $description = 'Call route from CLI';

public function __construct()
{
parent::__construct();
}

public function fire()
{
$request = Request::create($this->option('uri'), 'GET');
$this->info(app()['Illuminate\Contracts\Http\Kernel']->handle($request));
}

protected function getOptions()
{
return [
['uri', null, InputOption::VALUE_REQUIRED, 'The path of the route to be called', null],
];
}

}

You then need to register the command by adding it to the $commands array in app/Console/Kernel.php:

protected $commands = [
...,
'App\Console\Commands\CallRoute',
];

You can now call any route by using this command:

php artisan route:call --uri=/route/path/with/param

Mind you, this command will return a response as it would be sent to the browser, that means it includes the HTTP headers at the top of the output.

viewing output message in Laravel with echo

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.



Related Topics



Leave a reply



Submit