I have trouble in counting values in laravel 8. I want to count all in the table
below that have in and doesnt have a status out yet. How to count value that is doesnt have status out in laravel eloquent it is group by member_Code
you can count using DB::raw like this:
$values= MyModel::query()->where('status', '!=', 'out')
->select(['member_Code',DB::raw('count(*)')]) ->groupBy('member_Code')->get();
if you want to count all member that don't time out yet, you can use distinct method:
$inMemebersCount= MyModel::query()->where('status', '!=', 'out')
->select('member_Code')->distinct()->count();
The simple eloquent solution would be to use count() method like
Model::where('status', '!=', 'out')->count();
But what do you mean by group by? Do you want the count of each group?
try this:
$data = Model::where('status', '!=', 'out')->group_by('member_Code')->get();
$count = count($data);
$memberCode = 'G-51405';
$membersCount = Member::where('status','in')->where('member_code', $memberCode)->count();
Related
I want to get all the data from SALES and PURCHASES table but I want to show product name for the single time and also want to show the sum of its quanitity..
How can i do this in laravel 8 ?
What will be the query ??
$stock = DB::table('products')
->join('purchases','purchases.product_id','products.id')
->join('sales','sales.product_id','products.id')
->groupBy('product_id')
->get();
I dont have your whole query but use havingRaw to find the duplicates.
$dups = DB::table('tableName')
->select('col1_name','colX_name', DB::raw('COUNT(*) as `count`'))
->join('purchases','purchases.product_id','products.id')
->join('sales','sales.product_id','products.id')
->groupBy('col1_name', 'ColX_name')
->havingRaw('COUNT(*) > 1')
->get();
Maybe something like that
$stock = DB::table('products')
->select('sales.*','purchases.*','products.name', DB::raw('products.name as `NbProduct`'))
->join('purchases','purchases.product_id','products.id')
->join('sales','sales.product_id','products.id')
->groupBy('sales.id', purchases.id)
->get();
How can I select, preferably using eloquent laravel's query builder, the latest distinct records from a table.
I have tried this query without success.
Products::where('depart_id', $depart_id)
->distinct("serial_number")
->latest()
->where('serial_number', ""); // get one of the distinct products
You can try this: distinct() will get the distinct 'serial_number'
Products::where('depart_id', $depart_id)
->where('serial_number', "")
->distinct();
The solution was to use
Products::where('depart_id', $depart_id)
->distinct('serial_number')
->orderBy('serial_number', 'desc')
->orderBy('created_at', 'desc')
->where('serial_number', "serial_number");
This ensured that the distinct returned only the unique latest record of each value.
I have a column move_out that stores date in this format - m/Y e.g 02/2020. I want to order the records in that table by the move_out column in descending order but my solution don't seem to be working. It does not arrange the records appropriately. This is what I am doing.
$data = User::orderBy('move_out', 'DESC')->get();
How do I solve this?
The datatype for the move_out column is string.
try STR_TO_DATE
$data = User::orderBy(DB::raw("STR_TO_DATE(CONCAT('01-', move_out),'%d-%m/%Y')"), 'DESC')->get();
Try to use DATE_FORMAT:
$items = DB::table("users")
->orderBy(DB::raw("DATE_FORMAT(move_out,'%M/%Y')"), 'DESC')
->get();
I'm new to laravel and I have some issues with the query builder.
The query I would like to build is this one:
SELECT SUM(transactions.amount)
FROM transactions
JOIN categories
ON transactions.category_id == categories.id
WHERE categories.kind == "1"
I tried building this but it isn't working and I can't figure out where I am wrong.
$purchases = DB::table('transactions')->sum('transactions.amount')
->join('categories', 'transactions.category_id', '=', 'categories.id')
->where('categories.kind', '=', 1)
->select('transactions.amount')
->get();
I would like to get all the transactions that have the attribute "kind" equal to 1 and save it in a variable.
Here's the db structure:
transactions(id, name, amount, category_id)
categories(id, name, kind)
You don't need to use select() or get() when using the aggregate method as sum:
$purchases = DB::table('transactions')
->join('categories', 'transactions.category_id', '=', 'categories.id')
->where('categories.kind', '=', 1)
->sum('transactions.amount');
Read more: http://laravel.com/docs/5.0/queries#aggregates
If one needs to select SUM of a column along with a normal selection of other columns, you can sum select that column using DB::raw method:
DB::table('table_name')
->select('column_str_1', 'column_str_2', DB::raw('SUM(column_int_1) AS sum_of_1'))
->get();
You can get some of any column in Laravel query builder/Eloquent as below.
$data=Model::where('user_id','=',$id)->sum('movement');
return $data;
You may add any condition to your record.
Thanks
MyModel::where('user_id', $_some_id)->sum('amount')
MySQL has a feature for getting the total number of records a query would return without a limit, SQL_CALC_FOUND_ROWS. Does Laravel support this?
Currently I have to do it in two queries:
public function dataTable() {
$bookings = DB::table('bookings')
->limit(Input::query('iDisplayLength'))
->offset(Input::query('iDisplayStart'))
->get();
$count = $bookings = DB::table('bookings')
->count();
return Response::json([
'iTotalRecords' => $count,
]);
}
Not only will this be less efficient, but there's going to be a lot of redundant code once I add in all the ->where() criteria.
For any complicated or vendor-specific queries, you generally have to pass the query directly with DB::raw(), e.g.:
$bookings = DB::table('bookings')
->select(DB::raw('SQL_CALC_ROWS_FOUND ...
Refined what #giaour said.
$bookings = DB::table('bookings')
->select(array(DB::raw('SQL_CALC_FOUND_ROWS booking_id,name')))
->where('...')
->orWhere('...')
->take(10)
->skip(50)
->get();
$bookingsCount = DB::select( DB::raw("SELECT FOUND_ROWS() AS Totalcount;") );
After SQL_CALC_FOUND_ROWS you can fetch count by SQL_CALC_FOUND_ROWS();
I came across this issue because I wanted to paginate a set of results which already included a GROUP BY and Laravel's built-in paginate() method uses COUNT(*) and doesn't work with GROUP BY.
So I ended up extending the Builder class to inject the SQL_CALC_FOUND_ROWS right before a query is run, and run FOUND_ROWS right after. I did this by overriding the Laravel getModels() method.
I've uploaded the code here.