Can I log from a Laravel blade template? - php

I want to log from within a Laravel blade template. Can I log something using the Laravel logger, or a simple syslog(1, 'my message') would even be better.

You can use logger in blade
{{ logger('Test') }}

You can use laravel helper function
for store information on log use info function
{{ info("print log in blade")}}
for debug level message on log use logger function
Don't add semi colon in the logger function.
//{{ logger('Debug message'); }}
{{ logger('Debug message') }}

You can also do below, especially in Laravel 4.2
And try to load that blade then view your laravel.log file

If you are using a recent version of Laravel, use Telescope.
Link to Install Telescope: https://laravel.com/docs/7.x/telescope
After installing telescope, use the following in any part of your blade file:
Log::debug('Debug Message Goes Here'.)

Related

Laravel 5.2 Auth with TwigBridge Session store not set

As mentioned in the title I'm using Laravel 5.2 and I've set up TwigBridge so I can use twig for my views. I'm just using the basic Auth package that comes with Laravel. The problem is when I use twig templates I get the following error:
An exception has been thrown during the rendering of a template ("Session store not set on request.") in "...resources/views/auth/register.twig" at line 17.
This is pointing to a use of the {{ old('name') }} call. I've tried switching it to input_old as I think TwigBridge prefers that, but that didn't help. If I use the blade template there's no problem though. I'm not doing anything special either. I just rename the blade template so it isn't called, and my register twig template is called instead since it uses the name register.
The old values are fetch from the session. To get these values to store in session & fetch these values back, you need to apply \Illuminate\Session\Middleware\StartSession and
\Illuminate\View\Middleware\ShareErrorsFromSession middlewares to your routes. Both of these are a part of middleware group web.
You can apply web middleware group to all your routes(unless you are creating an API), like this:
Route::group(['middleware' => ['web']], function () {
Route::get("login" , "AuthController#getLogin");
//Similarly all other routes here
});

Laravel 5: Class 'HTML' not found

I'm just getting started with Laravel. I'm in a controller method and I say:
return \View::make('scrape', $data);
Then in scrape.blade.php I have:
#extends('layouts.master');
Finally, in layouts/master.blade.php I have:
{{ HTML::style('css/bootstrap.min.css') }}
And that where things seem to fall apart and I get:
FatalErrorException in 002eb18bb71fd3ec1de058967b799d49 line 6:
Class 'HTML' not found
What am I doing wrong? Thanks for your help.
Searching on google I found this
"By default in Laravel 5.0, Html and Form are not embedded anymore."
You need to add this package to you application.
Please use above links and last change HTML to Html.
eg:
{{ HTML::style('css/bootstrap.min.css') }}
to
{{ Html::style('css/bootstrap.min.css') }}.
its working.

Using Settings::get('var_name') in Laravel template instead of Config::get('file.var')

I would like to store my application settings in database.
In order to get a variable in template, I'm currently using
{{ Config::get('file.variable) }}
and settings are stored in config/file
I would like to create controller SettingsController with public static get and set methods and get variables in template in this way:
{{ Settings::get('var_name') }}
instead of
{{ SettingsController::get('var_name') }}
But I'm getting error: Class 'Settings' not found.
I've tried to set routes:
Route::controller('Settings', 'SettingsController'); and
Route::resource('Settings', 'SettingsController');
But none of the methods works.
Any ideas how to solve this problem?
This should be done using facades, which is already answered here:
How to create custom Facade in Laravel 4

Class 'Sentinel' not found (Laravel 4)

I'm using Laravel 4 and rydurham/Sentinel (https://cartalyst.com/manual/sentinel). I'm able to log in users, register users and display the Sentinel views I've customized perfectly fine. The config changes I've made register fine. However, when I include:
{{ Sentinel::check() }}
or
{{ Sentinel::guest() }}
I get the error Class 'Sentinel' not found. I notice there is no Sentinel Facade in $aliases, and running composer dump-autoload does not fix this. Any thoughts?
The problem was that I'm using rydurham/Sentinel and not Cartalyst's Sentinel package. They are two different things. rydurham/Sentinel includes the facades for Sentry though, so you can use:
{{ Sentry::check() }}
{{ Sentry::guest() }}

Laravel Passing variable not working

Hello People here is my code i have used in controller...
public function bulk()
{
return View::make('bulk')->with('message','hii there');
}
my route file contains...
Route::get('bulk',array('uses'=>'HomeController#bulk'))->before('auth');
In my view Iam testing it by ...
#if(Session::has('message'))
Present
#else
not Present
#endif
The page is making a view with the message 'not Present' why is it??
I even tried
return Redirect::to('bulk')->with('message','hii there');
I get an erro mesage on Console
mypro/public/bulk net::ERR_TOO_MANY_REDIRECTS
What could be the problem?? is there any issues with name?? I tried this method earlier which worked fine for me.... :(
Iam using Blade Template..
You are confusing Redirect and View. You use Session::get to access variables passed to redirects. For views (as in your case), with will pass an simple PHP variable into your view. So your check should be:
#if(isset($message))
{{{ $message }}}
#else
No message!
#endif
Read more here
As per the docs, you need to access the "with" value in the view using a PHP variable, it's not passed via the session. In your case, that would be $message. If you want to use the session, you should use Session::flash() or Session::put().

Categories