|
1
|
|
|
<?php namespace Arcanesoft\Blog\ViewComposers\Front\Widgets; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanesoft\Blog\Entities\PostStatus; |
|
4
|
|
|
use Arcanesoft\Blog\Models\Category; |
|
5
|
|
|
use Arcanesoft\Blog\ViewComposers\AbstractComposer; |
|
6
|
|
|
use Carbon\Carbon; |
|
7
|
|
|
use Illuminate\Contracts\View\View; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class CategoriesWidgetComposer |
|
12
|
|
|
* |
|
13
|
|
|
* @package Arcanesoft\Blog\ViewComposers\Front\Widgets |
|
14
|
|
|
* @author ARCANEDEV <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class CategoriesWidgetComposer extends AbstractComposer |
|
17
|
|
|
{ |
|
18
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
19
|
|
|
| Constants |
|
20
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
21
|
|
|
*/ |
|
22
|
|
|
const VIEW = 'blog::front._composers.widgets.categories-widget'; |
|
23
|
|
|
|
|
24
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
25
|
|
|
| Main Functions |
|
26
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
27
|
|
|
*/ |
|
28
|
|
|
/** |
|
29
|
|
|
* Compose the view. |
|
30
|
|
|
* |
|
31
|
|
|
* @param \Illuminate\Contracts\View\View $view |
|
32
|
|
|
*/ |
|
33
|
|
|
public function compose(View $view) |
|
34
|
|
|
{ |
|
35
|
|
|
$categories = Category::with('posts') |
|
|
|
|
|
|
36
|
|
|
->whereHas('posts', function (Builder $b) { |
|
37
|
|
|
$b->where('status', PostStatus::STATUS_PUBLISHED) |
|
38
|
|
|
->where('publish_date', '<=', Carbon::now()); |
|
39
|
|
|
}) |
|
40
|
|
|
->get() |
|
41
|
|
|
->sortByDesc(function($category) { |
|
42
|
|
|
return $category->posts->count(); |
|
43
|
|
|
}); |
|
44
|
|
|
|
|
45
|
|
|
$view->with('categoriesWidgetItems', $categories); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: