Search code examples
phplaraveleloquenteloquent-relationship

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 3.loans 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 Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, string given, called in C:\xampp\htdocs\120\120\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php on line 919


Solution

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

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