Laravel using View Composer in my routes with htmlspecial error

耗尽温柔 提交于 2019-12-11 01:50:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!