php - 如何在 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]);
}
解决方案
执行此操作后,您应该会收到下一个错误
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();
推荐阅读
- mongoose - 何时使用模式实例方法和静态
- python-3.x - 如何在python中准确地乘以大数
- javascript - 在 $(selector).load(URL) 之后表格准备好时运行代码
- mysql - 为什么phpmyadmin和mysql docker容器之间的连接不起作用
- sql-server - 从 SQL Server 2016 SP1 中的 QueryStore 获取报表服务器查询
- php - 不正确的条件导致 php [已解决]
- php - 将数据存储在 localStorage 并保存在数据库中
- php - 基于 WooCommerce 中购物车商品数量的额外价格
- android - 如何在 androidx.recyclerview.widget 中使用 androidx.recyclerview.selection。或者如何在 android 中使用 kotlin 在 recyclerview 中选择一个项目?
- php - WooCommerce get_attributes 仅返回一个属性的值