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
|
|
|
|
23
|
|
|
const VIEW = 'blog::front._composers.widgets.categories-widget'; |
24
|
|
|
|
25
|
|
|
/* ----------------------------------------------------------------- |
26
|
|
|
| Main Methods |
27
|
|
|
| ----------------------------------------------------------------- |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Compose the view. |
32
|
|
|
* |
33
|
|
|
* @param \Illuminate\Contracts\View\View $view |
34
|
|
|
*/ |
35
|
|
|
public function compose(View $view) |
36
|
|
|
{ |
37
|
|
|
$categories = Category::with('posts') |
|
|
|
|
38
|
|
|
->whereHas('posts', function (Builder $b) { |
39
|
|
|
// TODO: Try to export this code with model's scope ? |
40
|
|
|
$b->where('is_draft', false) |
41
|
|
|
->where('published_at', '<=', Carbon::now()); |
42
|
|
|
}) |
43
|
|
|
->get() |
44
|
|
|
->sortByDesc(function(Category $category) { |
45
|
|
|
return $category->posts->count(); |
46
|
|
|
}); |
47
|
|
|
|
48
|
|
|
$view->with('categoriesWidgetItems', $categories); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
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: