问题
I was trying to do a query in my routes for the view composer to load all the records and count it and display it in my sidebar for purposes.
I think there is no problem with the query since it worked in my shell using the php artisan tinker for testing purposes of the query.
But I got this error when I try to do it in my routes and pass it to the menu.blade.php
Here is the code in my routes with the view composer function.
// Menu View for Microbiologist
View::composer('microbiologist-dashboard.layouts.menu', function($view)
{
$view->with('counts', [
'microbiologist_task' => App\Models\AnalysisRequest::where('status', 'under_analyzation')
->whereHas('actors', function ($query) {
$microbiologist = Auth::guard('microbiologist')->user()->id;
$query->where('microbiologist_id', $microbiologist)->count();
})
]);
});
and here is the code in my menu.blade.php
<span class="pull-right-container">
@if($counts['microbiologist_task'])
<small class="label pull-right bg-yellow">{{ $counts['microbiologist_task'] }}</small>
@else
@endif
</span>
Here is the screenshot of the error.
is there something wrong with the config of my project about UTF-8? or should I use some helpers. Thanks btw I am using Laravel 5.5
Appreciate if someone could help. Thanks in advance.
回答1:
That's because you're trying to display an instance of AnalysisRequest
and not an integer. Use proper syntax:
$view->with('counts', [
'microbiologist_task' => App\Models\AnalysisRequest::where('status', 'under_analyzation')
->whereHas('actors', function ($query) {
$microbiologist = Auth::guard('microbiologist')->user()->id;
$query->where('microbiologist_id', $microbiologist);
})
->count();
]);
来源:https://stackoverflow.com/questions/48701597/laravel-using-view-composer-in-my-routes-with-htmlspecial-error