在 laravel 5.4 中的整数上调用成员函数 whereBetween()?
Call to a member function whereBetween() on integer in laravel 5.4?
我试图在两个不同的日期之间使用 whereBetween,但出现错误 "Call to a member function whereBetween() on integer"。我正在使用 bootstrap datepicker 从用户那里获取 2 个日期输入。我的代码如下:
我的表格如下:
<div class="form-group">
<label for="start_date">From:</label><input type="text" name="startdate" id="startdate">
</div>
<div class="form-group">
<label for="enddate">To:</label><input type="text" id="enddate" name="enddate">
</div>
我的查询如下:
$client_ids = Array
(
[0] => 1
[1] => 5
)
$start = 2017-06-01;
$end = 2017-06-02;
$results = DB::table('lead_audit AS l')
->leftjoin('suppliers AS s', 's.id', '=', 'l.supplier_id')
->leftjoin('campaigns AS c', 'c.id', '=', 'l.campaign_id')
->select('s.name AS Supplier', 'c.name AS Campaign', 'l.disposition AS Disposition')
->count()
->whereBetween('l.received', [$start, $end])
->whereIn('l.client_id', $client_ids)
->groupBy('s.name', 'c.name', 'l.disposition')
->orderBy('s.name')
->orderBy('c.name')
->orderBy('l.disposition')
->get();
我用谷歌搜索了一下,我尝试了如下 "Wrap the dates in Carbon objects" 的解决方案
在我的控制器顶部,我已经放置了这一行 "use Carbon\Carbon;" 并更改了 whereBetween 处的查询,如下所示:
->whereBetween('l.received', [new Carbon($start), new Carbon($end)])
但运气不好,再次出现与 "Call to a member function whereBetween() on integer" 相同的错误。我也怀疑上面的完整查询的顺序是否正确?有人可以帮我解决这个问题吗?谢谢
问题是 ->count()
后跟 ->whereBetween()
。
函数 count()
给出 select()
的行数,因此它 returns 是一个整数。
您必须删除 ->count()
并且查询应该可以工作。如果您想要查询中的行数,您可以执行以下操作:
$results = DB::table('lead_audit AS l')
->leftjoin('suppliers AS s', 's.id', '=', 'l.supplier_id')
->leftjoin('campaigns AS c', 'c.id', '=', 'l.campaign_id')
->select('s.name AS Supplier', 'c.name AS Campaign', 'l.disposition AS Disposition')
->whereBetween('l.received', [$start, $end])
->whereIn('l.client_id', $client_ids)
->groupBy('s.name', 'c.name', 'l.disposition')
->orderBy('s.name')
->orderBy('c.name')
->orderBy('l.disposition')
->get();
$num_rows = count($results);
The query builder also provides a variety of aggregate methods such as count
, max
, min
, avg
, and sum
. You may call any of these methods after constructing your query.
source: https://laravel.com/docs/5.4/queries#aggregates
我试图在两个不同的日期之间使用 whereBetween,但出现错误 "Call to a member function whereBetween() on integer"。我正在使用 bootstrap datepicker 从用户那里获取 2 个日期输入。我的代码如下:
我的表格如下:
<div class="form-group">
<label for="start_date">From:</label><input type="text" name="startdate" id="startdate">
</div>
<div class="form-group">
<label for="enddate">To:</label><input type="text" id="enddate" name="enddate">
</div>
我的查询如下:
$client_ids = Array
(
[0] => 1
[1] => 5
)
$start = 2017-06-01;
$end = 2017-06-02;
$results = DB::table('lead_audit AS l')
->leftjoin('suppliers AS s', 's.id', '=', 'l.supplier_id')
->leftjoin('campaigns AS c', 'c.id', '=', 'l.campaign_id')
->select('s.name AS Supplier', 'c.name AS Campaign', 'l.disposition AS Disposition')
->count()
->whereBetween('l.received', [$start, $end])
->whereIn('l.client_id', $client_ids)
->groupBy('s.name', 'c.name', 'l.disposition')
->orderBy('s.name')
->orderBy('c.name')
->orderBy('l.disposition')
->get();
我用谷歌搜索了一下,我尝试了如下 "Wrap the dates in Carbon objects" 的解决方案 在我的控制器顶部,我已经放置了这一行 "use Carbon\Carbon;" 并更改了 whereBetween 处的查询,如下所示:
->whereBetween('l.received', [new Carbon($start), new Carbon($end)])
但运气不好,再次出现与 "Call to a member function whereBetween() on integer" 相同的错误。我也怀疑上面的完整查询的顺序是否正确?有人可以帮我解决这个问题吗?谢谢
问题是 ->count()
后跟 ->whereBetween()
。
函数 count()
给出 select()
的行数,因此它 returns 是一个整数。
您必须删除 ->count()
并且查询应该可以工作。如果您想要查询中的行数,您可以执行以下操作:
$results = DB::table('lead_audit AS l')
->leftjoin('suppliers AS s', 's.id', '=', 'l.supplier_id')
->leftjoin('campaigns AS c', 'c.id', '=', 'l.campaign_id')
->select('s.name AS Supplier', 'c.name AS Campaign', 'l.disposition AS Disposition')
->whereBetween('l.received', [$start, $end])
->whereIn('l.client_id', $client_ids)
->groupBy('s.name', 'c.name', 'l.disposition')
->orderBy('s.name')
->orderBy('c.name')
->orderBy('l.disposition')
->get();
$num_rows = count($results);
The query builder also provides a variety of aggregate methods such as
count
,max
,min
,avg
, andsum
. You may call any of these methods after constructing your query.
source: https://laravel.com/docs/5.4/queries#aggregates