John Davidson

php - 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
Illuminate\Database\Query\Builder::cleanBindings() must be of the type
array, string given, called in
on line 919

My Answer:
>select(DB::raw('count(id) as count'))->first();

This will return the count of instances of the 'id' column in the 'emprunts' table and store it in the variable $data.

Rate this post

4 of 5 based on 9948 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us