Php 有人能给我解释一下这个拉威尔密码吗?

Php 有人能给我解释一下这个拉威尔密码吗?,php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,我有一些如下的代码,我想问的是完全相同的//dd($example->count())#10为什么把dd()放在不同的行上有不同的值?什么更改了我的$example事件?我从未重新分配它 $example = $car->wheels()->whereBetween( 'created_at', [ $starDay->format('Y-m-d h:i:s'), $today->format

我有一些如下的代码,我想问的是完全相同的
//dd($example->count())#10
为什么把
dd()
放在不同的行上有不同的值?什么更改了我的
$example
事件?我从未重新分配它

$example = $car->wheels()->whereBetween(
        'created_at',
        [
            $starDay->format('Y-m-d h:i:s'),
            $today->format('Y-m-d h:i:s')
        ]
    )

$total =  $example->count();

// dd($example->count()) #10

$totalSuccess = $example->where('status', 'good')->count();

// dd($example->count()) # 5

$colors = $example->select('color', DB::raw('count(*) as total'))
        ->groupBy('color')
        ->get()
        ->toArray();

// dd($example->count()) # []

由于每次向查询中添加越来越多的不同子句(如
where
),该值都会发生变化。这些调用实际上会更改查询对象本身,并且更改会持续

  • 首先,您在
    $example
    中有一个查询对象,其中只有
    wherebeween
    子句。它返回数据库中10行的计数
  • 然后将
    where('status','good')
    添加到查询中,它将选择范围缩小到5行
  • 最后,使用
    select(…)
    groupBy()
    调用更改
    $example
    查询
  • 在Laravel中,向查询生成器添加查询构造时,对象会发生变化。因此,当您调用
    $example->where(…)
    时,您的
    $example
    查询生成器对象现在将具有
    where
    子句。

    我猜
    count()
    是一个对象方法。每次调用
    count()
    时,都会更改
    $example
    对象
    count
    属性。这就是为什么我们调用相同的
    dd($example->count())
    但返回不同的值的原因。。。