John Davidson

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

0 comments
Message:


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




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

3 of 5 based on 8951 votes

Comments




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