首页 > 解决方案 > 如何在 laravel 8 中计算表中的记录

问题描述

我想要实现的是在db_table中不存在来自表单的所有搜索词保存在db中。

它不起作用的是第三步,如下面的评论所示。

    public function search(Request $request){
        $q = $request->get('q', 'Δεν υπαρχει');
        if (!$request->filled('q')) $q = 'Δεν υπαρχει';
        else{ 
            //1 First save in db all search terms @ table Searches
            $search_term = new SEARCH();
            $search_term->term = $request->get('q');
            $search_term->save();
            //2 Then return all relevant eshops
            $eshops = Eshop::orWhere('tags', 'like', '%'.$q.'%')->
                             orWhere('title', 'like', '%'.$q.'%')->
                             orWhere('link', 'like', '%'.$q.'%')->
                             get()->count();

            $count = $eshops->count();

            if($count == 0){
            //3 Lastly save in db all not found search terms @ table notfound
            $not_found_search_term = new NOTFOUND();
            $not_found_search_term->term = $request->get('q');
            $not_found_search_term->save();
            }

        }
        return view('eshops', ['eshops' => $eshops]);
    }

标签: phplaravel

解决方案


执行此操作后,您应该会收到下一个错误 Call to a member function count() on int

这是因为你复制了->count().

$eshops = Eshop::orWhere('tags', 'like', '%'.$q.'%')->
                             orWhere('title', 'like', '%'.$q.'%')->
                             orWhere('link', 'like', '%'.$q.'%')->
                             get()->count();

            $count = $eshops->count();

推荐阅读