Ehi there, I'm new in laravel and I have some issues with laravel query builder.The query I would like to build is this one: SELECT SUM(transactions.amount)FROM transactionsJOIN categoriesON transactions.categoryid categories.idWHERE categories.kind '1'I tried building this but isn't working and I can't figure out where I am wrong. $purchases = DB::table('transactions')-sum('transactions.amount')-join('categories', 'transactions.categoryid', '=', '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, categoryid)categories( id, name, kind). I am so sorry! I didn't knew I had to do that, I'm a newbie in stackoverflow!
I am having other issues in my code, and maybe you can help me out. Sorry but I don't really get laravel query building methods. I have this query, which takes all the transactions that have categories with the kind equal to 1 and then I print the results to show them to the user. The problem is that all users can see them! The relations between the main tables are good, I guess something is wrong with the query, probably I should get the transactions created by the connected user. What should I change?–Jun 6 '15 at 0:15.
We mostly required to get sum of amount, salary etc in laravel. We can also get sum of column using mysql SUM. We have two way to get sum of column value. First we can use laravel sum of query builder and another one we can use with directly with select statement using DB::raw. I give you both example you can see and use any one as perfect for you.Example 1:$data = DB::table('click')-sum('numberofclick');printr($data);Example 2:$data = DB::table('click')-select(DB::raw('SUM(numberofclick) as count'))-orderBy('createdat')-groupBy(DB::raw('year(createdat)'))-get;printr($data).
Cara Menggunakan Cmd
Mungkin bisa di coba seperti ini,$results = DB::table('MSTBEASISWAS')-select(DB::raw('NMPERUSAHAAN,count(.) as totalcount,sum(FLAGTERIMA) as approved,sum(1 - FLAGTERIMA) as notapproved'))-groupBy('NMPERUSAHAAN')-get;dd($results);// atau bisa juga menggunakan query mentah sql, kira2 seperti ini$results1 = DB::select('SELECT NMPERUSAHAAN,count(.) as totalcount,sum(FLAGTERIMA) as approved,sum(1 - FLAGTERIMA) as notapprovedfrom MSTBEASISWASgroup by NMPERUSAHAAN');dd($results1);Belum di tes, tp semoga bisa membantu.
Sometimes we require to make subquery inside the select statement in Laravel. At that time we are thinking how to select from subquery using in Laravel Query Builder.In this post i will give you example of subquery in select statement in Laravel 5 application from scratch. For subquery in select statement we are going to use DB::raw. DB raw function through we can simply make suquery join in Laravel Eloquent Query Builder.Here as bellow full example, i have three tables as listed bellow:1)products2)productsstock3)productssellFrom this three tables, i require to get products with sum of stock and sell of each records. Read Also:IlluminateSupportCollection Object(items:protected = Array(0 = stdClass Object(id = 1name = Goldcreatedat = 2016-09-21 17:37:53updatedat = 2016-09-21 17:37:53productstock = 150productsell = 30)1 = stdClass Object(id = 2name = Silvercreatedat = 2016-09-21 17:37:53updatedat = 2016-09-21 17:37:53productstock = 110productsell = 10)2 = stdClass Object(id = 3name = Diamondcreatedat = 2016-09-21 17:37:53updatedat = 2016-09-21 17:37:53productstock = 200productsell = 60)))I hope it can help you.
Cara Menggunakan Sum Di Laravel 4
We mostly required to get sum of amount, salary etc in laravel. We can also get sum of column using mysql SUM. How to make windows 10 home transparent. We have two way to get sum of column value. First we can use laravel sum of query builder and another one we can use with directly with select statement using DB::raw.
I give you both example you can see and use any one as perfect for you.Example 1:$data = DB::table('click')-sum('numberofclick');printr($data);Example 2:$data = DB::table('click')-select(DB::raw('SUM(numberofclick) as count'))-orderBy('createdat')-groupBy(DB::raw('year(createdat)'))-get;printr($data).
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |