Codex CLI Has Responsive Tables in the Terminal
LLMs use Markdown tables all the time. A few months ago, Codex CLI displayed them as ordinary wrapped text. In a narrow…
Imagine you have a Laravel component that fetches data from the database. This component is used multiple times in different parts of your view, like this:
<!-- In your Blade view -->
@include('components.data-fetcher', ['type' => 'users'])
@include('components.data-fetcher', ['type' => 'posts'])
@include('components.data-fetcher', ['type' => 'comments'])
If each inclusion of this component runs its own database query, you're potentially running the same query multiple times per page load. This is inefficient and can slow down your application.
We can optimize this by ensuring the database query runs only once per request, regardless of how many times the component is used. Here's how:
Here's how we can implement this pattern:
class DataFetcher
{
private static $instance = null;
private $data = null;
public function __construct()
{
if (self::$instance === null) {
self::$instance = $this;
} else {
$this->data = self::$instance->data;
}
}
public function getData($type)
{
if ($this->data === null) {
$this->data = $this->fetchAllData();
}
return $this->data[$type] ?? [];
}
private function fetchAllData()
{
// This query runs only once per request
return [
'users' => DB::table('users')->get(),
'posts' => DB::table('posts')->get(),
'comments' => DB::table('comments')->get(),
];
}
}
And in your component view:
@php
$dataFetcher = new DataFetcher();
$data = $dataFetcher->getData($type);
@endphp
<!-- Display $data here -->
$instance property ensures only one instance of DataFetcher exists per request.DataFetcher objects created in the same request share the same $data.getData() is first called, not on object creation.DataFetcher class. The way you use the component in your views doesn't change.This pattern is useful when:
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com