Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Darkdawg's avatar

Passing common data to views with mixed Blade/Livewire/Volt

I have a hybrid setup where a Page model contains page metadata, but the actual content lies within the views. Metadata may include stuff like title and meta description. The unique identifier is the url path.

My issue is that I have a mix of normal, Volt & Livewire routes. What's the best way to pass data to the views here, while also keeping it DRY? I was considering view composers, but I haven't used them before - would they be appropriate in this case?

// This needs to fetch data from somewhere, for example from the rendering(View $view) method
Volt::route('/profile', 'settings.profile')->name('profile');

// This also needs to fetch it from somewhere, perhaps a Controller instead?
Route::view('/', 'pages.my-page.index')->name('index');

I could use controllers for both cases, and then just render the Volt component inside a normal blade view, but that feels like a lot of boilerplate?

1 like
6 replies
vincent15000's avatar

If you suggest using a controller, yes it makes sense.

Route::get('/', [MyController::class, 'index'])->name('index');
class MyController
{
	public function index()
	{
		$data = // from some query

		return view('pages.my-page.index', compact('data'));
	}
}

If you need to retrieve the same data from different places in your code, you can export the query into a service.

class DataService
{
	public function getData()
	{
		$data = // from some query

		return $data;
	}
}

And use the service in the controller, in Volt, ...

class MyController
{
	public $dataService;

	public function __construct()
	{
		$this->dataService = new DataService;
	}

	public function index()
	{
		$data = $this->dataService->getData();

		return view('pages.my-page.index', compact('data'));
	}
}
1 like
Snapey's avatar

Use view composers. This way you can ensure that EVERY view has access to certain properties and not worry about them in your components.

You can then output the data with regular blade tags in the layout file or in the components

2 likes
vincent15000's avatar

@Snapey Hmmm ... if I only have 2 or 3 views that need the same data, I prefer a service.

I usually use the view composer if I need some permanent data like auth user name, plan settings, ... that really need to be loaded on every view.

Darkdawg's avatar

@vincent15000 Well, I'm looking at a lot of views down the road, like 20-50+. If a view is a page then it needs the page metadata, and it rarely changes. Seems to fit your description?

Any page, like: front page, contact, about us, services, privacy policy, dashboard, profile, settings...

1 like
vincent15000's avatar

@Darkdawg You should do this with sharing data from the app service provider.

View::share('data', $datas);

Then the $data variable is accessible from all views.

Darkdawg's avatar

Thanks, both of you. I hadn't considered a service class. I think I will give view composers a go, though. Seems to make the most sense as every page needs it.

1 like

Please or to participate in this conversation.