Laravel 5.4 - 错误调用未定义的方法Illuminate \ Http \ JsonResponse :: count()

时间:2018-01-25 06:07:36

标签: php json laravel collections httpresponse

当我从编辑器将集合传递给我的视图时,我有时会收到此错误。

我在这一行有这个错误:

@if(!empty($running_out_offers->count()))
  .....
@endif

我不知道这有什么问题,无法调试,因为即使我没有操纵数据等,也会发生一些错误。

这是我的控制者:

namespace App\Http\Controllers;

use App\Enums\PostType;
use App\Models\Blog\BlogPost;
use App\Models\Campaign;

class WebsiteController extends Controller
{
    function homepage(){

      return view('website.homepage');
    }
}

这是我的作曲家:

function compose(View $view)
{

    if ($view->getName() == 'website.homepage') {
        $view->with([
            'running_out_offers'   => $this->runningOutOffers(4),
         ]);
     }
} 

private function runningOutOffers($limit = null,$fields = '')
{
    return $this->remember('runningOutOffers', 1, function () use ($limit,$fields) {
        return offerAndEntity($limit, function ($query) {
            $query->main()->runningOut();
        },null,$fields);
    });
}


   function remember($key, $time, $callback)
{
    return cache()->store('file')->remember('prefix_'.$key, $time, $callback);
}

这将返回一个集合。

有人能告诉我这是什么问题吗? :(

是否可以因为缓存?!

编辑:

{{dd($ running_out_offers)}}显示了这一点:

 Collection {#1038 ▼
  #items: array:1 [▼
  0 => Entity {#980 ▼
  #fillable: array:20 [▶]
  #attach: array:1 [▶]
  #presenter: "App\Presenters\EntityPresenter"
  #casts: array:1 [▶]
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:9 [▼
    "id" => 42807
    "title" => "Offer Title"
    "type" => "SERVICE"
    "is_parent" => 1
    "parent_id" => null
    "attachments" => "{"icon":"fdc839958987caad5ba67016fcd77eaf.jpg"}"
    "available_children" => 2
    "sold" => 21
    "is_available" => 1
  ]
  #original: []
  #dates: []
  #dateFormat: null
  #appends: []
  #events: []
  #observables: []
  #relations: array:1 [▶]
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
  #forceDeleting: false
  #public_path: "/home/shikoff/public_html"
  #upload_path: "/home/shikoff/public_html/upload/entity"
  #upload_relative_path: "upload/entity"
  #presenterInstance: null
}

] }

2 个答案:

答案 0 :(得分:1)

尝试 -

@if(count($running_out_offers)>0)
  .....
@endif

答案 1 :(得分:1)

您必须返回查询构建器才能使用count函数而不返回集合