Laravel / Eloquent : request that returns a count of id instances inside a table then stores it into a variable

I’m currently working on a project on laravel 8.
I have 3 tables : 1.customers 2.books
I want to be able to make a request like $data=DB::table('emprunts')->where('customerid',$id->number)->first(); but where it would count the instances of loans with this customerid and return it into a variable.

I tried with

$limitemprunts = Emprunt::WhereIn('clientid',$search_text)->where('clientid',$search_text)->distinct()->get()->count();

but it gives me this error :

TypeError Argument 1 passed to
IlluminateDatabaseQueryBuilder::cleanBindings() must be of the type
array, string given, called in
on line 919


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You will need to convert the second parameter of whereIn into an array:

$limitemprunts = Emprunt::WhereIn('clientid',[$search_text])->distinct()->get()->count();

Method 2

As said in the comment, the answer was really simple : feed it an array

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x