I want to sum with substring in laravel like this sql.
(My model name is Fgbarcode) Thanks for help me T^T
SELECT SUM(substr(fg_code, 22,14)) AS fg_code
FROM fg_barcode group BY substr(fg_code,2,3);
Assuming you have an eloquent model you are able to utilize DB raw to achieve these custom mysql queries.
FgBarcode::
select(DB::raw('SUM(substr(fg_code, 22,14)) AS fg_code'))
->groupBy(DB::raw('substr(fg_code,2,3)'));
Related
We are working on php laravel project where we find relational data using joins which we get correctly but we also want to find out count of this dafa which we get from joins but when we apply count function with joins then it gives same count value of all data which we get from database using joins.
this is the code and kindly let me know that what is the issue in this code thanks in advance.
I don`t get your question but I think you want count of the retrieve data.
1st method:
$variabel = Model::all();
View
$variabel->count();
2nd method:
$users = DB::table('users')->count();
I have a countif query in mysql and I would like to show the table on my html. I'm currently using laravel 6.0 framework.
Here is the picture of the table i want to show:
Here is my code in html:
Here is my code in the controller:
There should be numerous errors with index function in your controller. Specifically with how you are trying to assign $count a value. Read these: Eloquent Methods all() vs get(), Eloquent Selects, Eloquent Ordering, Grouping, Limit and Offset, Eloquent Where.
Laravel has an excellent documentation, if you were to follow it - working with Laravel would become much easier.
How to do in Laravel?
1) get from table rows where user_id has the equal values?
2) return sum of some_amount values from this selected rows
Table:
- id;
- user_id;
- some_amount;
Table has bunch of certificates with some amount of money that belongs to different users. I need to find all certificates that belongs to one user (one user can have few certificates) and count how much money he have from all his certificates
Given you aren't looking for a solution to query this for an individual user, it sounds like you want to group by the user and sum the result of certificates.
The answer from #PhilCross is pretty close, you'd just need to modify it to add the group clause and remove the where condition. Something like this:
ModelName::groupBy('user_id')->sum('some_amount');
or
\DB::table('table_name')->groupBy('user_id')->sum('some_amount');
Generally eloquent or the query builder will have a method that relates to how you would do this in raw SQL.
I find it helpful to write out or think about how I would write the raw SQL and then slowly fill the eloquent or query builder in from that.
If you use a Model:
ModelName::where('user_id', 1)->sum('some_amount')
If you're using the query builder:
\DB::table('table_name')->where('user_id', 1)->sum('some_amount');
This is the documentation for the query builder:
https://laravel.com/docs/5.6/queries
Models: https://laravel.com/docs/5.6/eloquent
Model Relationships: https://laravel.com/docs/5.6/eloquent-relationships
Collections: https://laravel.com/docs/5.6/collections
I am new to Laravel 5.4. I am facing a big problem to make decision whether I should focus on mySQL or Elequent or query builder to make complex query.
For example: I need a electric bill report based on different charge range.
0-99=>$120
100-199=>$200
200-299=>$220
300-399=>$250
How can make a elequent query or query builder to make a report with other data such as Customer name, address, phone, sum of total, with details list of which range is being charged to him etc?
Should I use raw SQL query for this?
As I am using AngularJs, I have to return all this in a single query. If you want to help me using a controller, you can show me.
What can I do?
I used .Net where I can make this easily by LINQ query, but I am stuck here!
I think laravel query builder is not powerful like LINQ. Leave .Net if you are not familiar with it.
Anyways, I need help to get out of the problem. How can I write that complex query?
Desired result:
Suppose I have Customers table, Billranges table and Bill table
I need:
Customername-Address-Metter-Charge-Bill
John-Dhaka-250-$220 -55000(250x220)
Can I do this in Laravel 5.4 Query builder?
I'm trying to get data from multiple relationships. But I don't want to end up with multiple queries. So the with method won't work for me.
I want to use joins to get the data needed, but laravel overwrites keys if they are duplicated. Is there a way to save the results from a join as a relation. Something like this below (join is incorrect I know).
Post::select('post.*', 'category.*')->join('category');
If both have an 'id' field it's overwritten by the other. So I would like to have category.* results as a relation so I can call ->category->id like I can when I use the with method.
Is there any way to do this?
You may use the whereHas and orWhereHas methods
Check this link:
http://laravel.io/forum/04-19-2014-many-to-many-to-many-in-wherehas
And Laravel Documentation:
https://laravel.com/docs/5.1/eloquent-relationships