Skip to content
Luca Ubiali Web Developer

Understanding Laravel's Context Capabilities - Introducing Context

September 4th, 2024
Continuous Learning

Episode - https://laracasts.com/series/understanding-laravels-context-capabilities/episodes/1


Context is a data store, similar to Session. The difference is that Context is not persisted across requests.

Whatever is added to Context will show up automatically when using the Log facade.

1Context::add('account_id', $account->id);
2 
3Log::info('User changed account', [
4 'user_id' => $request->user()->id
5]);
6 
7/*
8* Logs will show:
9* local.INFO: User changed account {"user_id":3} {"account_id":7}
10*/

Data can be added to Context as “hidden“, so it won’t show up in logs:

1// write hidden data
2Context::addHidden('account_id', $account->id);
3 
4// retrieve hidden data
5Context::getHidden('account_id');