Skip to main content
  1. About
  2. For Teams
Asked
Viewed 599 times
1

Hello i have a table with 10 rows in total where 6 have ID 1 and 4 have ID 2 i want to count only the rows with ID 1 but my code counts all of them in total. This is my code:

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

If the code worked correct i should get 6 rows with specific $id but this code gives 10 rows that are total in the table.

If i do dd(clientHasOrder::all()); i get:

Result

This is the result in details as you can see i get the 10 results but i need only 6 rows with specific 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
}
2
  • 2
    You are doing a ->firstOrFail() which will give you the first entry or fail. You should use ->get();
    geertjanknapen
    –  geertjanknapen
    2023-01-31 08:07:53 +00:00
    Commented Jan 31, 2023 at 8:07
  • Explain this i have a table with 10 rows in total where 6 have ID 1 and 4 have ID 2 i want to count only the rows with ID 1 but my code counts all of them in total. This is my code:
    Abdulla Nilam
    –  Abdulla Nilam
    2023-01-31 08:52:09 +00:00
    Commented Jan 31, 2023 at 8:52

1 Answer 1

4

You can invoke count directly on the query, like this:

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

dd($clientOrder);

And that will show the correct amount, you do not need to firstOrFail, because if the results are empty, it will show 0 count.

Sign up to request clarification or add additional context in comments.

12 Comments

i tried this and this gives me only total of rows which for me is 10, i want to count rows with ID 6 only
The query above is 100% correct, what you might be running to is problems with your Model Name, or your actual data, you need to provide more data and information to diagnose why your model is returning all data instead of specific data. start by doing dd(clientHasOrder::all()); and post the results in your question by editing your question. Also I really recommend that you change your model name, to something like "ClientOrder"
i edited my post and added the result
please expand your items, and post the code not an image, I will edit and format it for you if you don't know how to do it :)
i posted it as code expanded.
|

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.