您好,我有一个总共有10行的表,其中6行有ID 1行,4行有ID 2行.我想只计算ID 1行的行,但我的代码总共计算了所有行.这是我的代码:

$clientOrder = clientHasOrder::where('order_id', $id)->firstOrFail();
        $rowCount = $clientOrder->count();
        dd($rowCount);

如果代码工作正常,我应该得到6行和特定的$id行,但这段代码给出了表中总共10行.

如果我做dd(clientHasOrder::all());,我会得到:

Result

这是详细的结果,如您所见,我得到了10个结果,但我只需要6行具有特定ID

Illuminate\Database\Eloquent\Collection {#1367 ▼ // app/Http/Controllers/OrderController.php:121
  #items: array:11 [▼
    0 => App\Models\clientHasOrder {#1369 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "36"
        "product_id" => 1
        "amount" => "10"
      ]
      #original: array:3 [▼
        "order_id" => "36"
        "product_id" => 1
        "amount" => "10"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    1 => App\Models\clientHasOrder {#1370 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "36"
        "product_id" => 2
        "amount" => "20"
      ]
      #original: array:3 [▼
        "order_id" => "36"
        "product_id" => 2
        "amount" => "20"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    2 => App\Models\clientHasOrder {#1371 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "36"
        "product_id" => 3
        "amount" => "30"
      ]
      #original: array:3 [▼
        "order_id" => "36"
        "product_id" => 3
        "amount" => "30"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    3 => App\Models\clientHasOrder {#1372 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "36"
        "product_id" => 4
        "amount" => "40"
      ]
      #original: array:3 [▼
        "order_id" => "36"
        "product_id" => 4
        "amount" => "40"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    4 => App\Models\clientHasOrder {#1373 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "37"
        "product_id" => 7
        "amount" => "10"
      ]
      #original: array:3 [▼
        "order_id" => "37"
        "product_id" => 7
        "amount" => "10"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    5 => App\Models\clientHasOrder {#1374 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "37"
        "product_id" => 8
        "amount" => "20"
      ]
      #original: array:3 [▼
        "order_id" => "37"
        "product_id" => 8
        "amount" => "20"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    6 => App\Models\clientHasOrder {#1375 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "37"
        "product_id" => 9
        "amount" => "30"
      ]
      #original: array:3 [▼
        "order_id" => "37"
        "product_id" => 9
        "amount" => "30"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    7 => App\Models\clientHasOrder {#1376 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "37"
        "product_id" => 10
        "amount" => "40"
      ]
      #original: array:3 [▼
        "order_id" => "37"
        "product_id" => 10
        "amount" => "40"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    8 => App\Models\clientHasOrder {#1377 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "38"
        "product_id" => 1
        "amount" => "10"
      ]
      #original: array:3 [▼
        "order_id" => "38"
        "product_id" => 1
        "amount" => "10"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    9 => App\Models\clientHasOrder {#1378 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "38"
        "product_id" => 7
        "amount" => "20"
      ]
      #original: array:3 [▼
        "order_id" => "38"
        "product_id" => 7
        "amount" => "20"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▶]
    }
    10 => App\Models\clientHasOrder {#1379 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "38"
        "product_id" => 9
        "amount" => "30"
      ]
      #original: array:3 [▼
        "order_id" => "38"
        "product_id" => 9
        "amount" => "30"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
  ]
  #escapeWhenCastingToString: false
}

推荐答案

您可以直接对查询调用count,如下所示:

$clientOrder = clientHasOrder::where('order_id', $id)->count();

dd($clientOrder);

这将显示正确的金额,您不需要FirstOr Fail,因为如果结果为空,它将显示0 count.

Laravel相关问答推荐

如何定制有关密码被泄露的Laravel Breeze S消息?

Livewire 3分页在点击后不更新页面

如何在Vite和Rollup中完全禁用分块?

在 Laravel 中设置多个 Vue 组件的问题

使用 Laravel 计算页面浏览量

向 Laravel 模型查询添加计算字段

Laravel:如何强制使用 HTTPS?

Laravel 5 Eloquent 范围连接并 Select 特定列

指令allow_call_time_pass_reference警告

Laravel Blade 模板 @foreach 订单

在 laravel 5 中的表单请求验证后传递旧输入

如何更改 Laravel5 中的视图文件夹?

Laravel 5.3 Electron邮件队列中不允许序列化关闭

升级到 Laravel 5.2 会使所有会话失效

Auth 在 Laravel Tinker 中不起作用

Laravel assets资源与混合

Laravel Passport 通过访问令牌获取客户端 ID

将参数传递给 Laravel 中的中间件

如何在 Laravel 5 请求类中使用有时规则

函数参数前的三个点代表什么?