PHP Laravel SQL Or Elequent Or Query builder - php

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?

Related

Query Builder for SphinxQL - access and compare columns against each other from the Match method

Am trying to search my sphinx index instance but can't seem to get it working with my current logic.
Basically, am using Query Builder for SphinxQL instead of the offical API but works pretty well until I tried comparing columns against each without any success.
Example 1: In MySQL
SELECT ID, NAME, NICKNAME WHERE NAME=NICKNAME;
Example 2: Query Builder for SphinxQL
$instance->select(...)->from('theIndex')->where('NAME', '=', 'NICKNAME');
The first example works well in mysql but the second one is using PHP to access SphinxQL and thus doesn't care that the value entered in the value field is actually a column. It never matches at all.
Is what am trying to achieve here even possible at all in sphinx?
I have spent way too much too time searching but can't find a viable solution.
Any help will be greatly appreciated.

Show countif mysql query table to html

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.

Laravel How to sum DB query with substring?

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)'));

Sum a subsection of a query result column?

I have an interesting problem and am having trouble figuring it out. I'm trying to optimize some SQL queries by handling some of the result separation with PHP rather than running multiple SQL queries. My query returns the following:
Using PHP, is there a way to sum the results by 'processor'? So, for example, I want be able to echo out '36.00' for PayPal and so forth. I'm currently able to do this with multiple individual queries, but am relatively confident there's a way to do this with one query and using PHP - I just can't figure out how to do it.
Thanks in advance for the help.
I think you just want a group by query:
select processor, sum(payment_amount)
from t
group by processor;
There is no reason to do this work in PHP.

Is it possible to join two query result with PHP code?

I need to JOIN 2 tables (lets say User & Order table) for reporting module in my web app.
The problems are:
The User table is located on the different server & different
DBMS from the Order table. Technically it is a different system, so the User table is located on SQL Server DB, meanwhile the Order table is located on MySQL DB.
I couldn't use SQL Server's Linked Server because my company policy doesn't allow it. So, I coudn't JOIN them directly with SQL code. They want me to use Web Service instead of linked server.
The result of JOIN operation from those tables has a large number of rows (maybe more than 10,000 rows because the data aimed for reporting). So, I think it was a horrible thing to mapping them using Web Service.
So I came up with this:
I collected 2 query result from different models and join them with my app code (I'm using PHP with CodeIgniter) :
// First result
$userData = $this->userModel->getAllUser();
// Second result
$orderData = $this->orderModel->getAllOrder();
The $userData contains all user entities with the following columns:
[UserId, Username, Address, PhoneNumber, etc..]
And the $orderData contains all order entities with the following columns:
[OrderId, UserId, Date, etc..]
But is it possible to join those two query results in PHP / CodeIgniter?
How about the performance regarding the large amount of data?
Should I just use Web Service as suggested or there's another solution to accomplish this?
Thanks in advance :)
A few things to think about:
Do you actually need to return all user and order records in one single go
Do you actually want to return all rows for these two types of record
Would you be better off with a Report module for these report queries?
Would plain SQL syntax be a smarter move than trying to shim this into existence with the CodeIgniter "Active Record" (renamed Query Builder in 3.0)
Is JOIN really so bad? It is not a UNION, you want the data to be related.
I would recommend you limit your data returns, SELECT only the fields you actually require, make a new Report model to avoid trying to mess up your generic models and do this with raw SQL.
Complicated things get all the more complicated when you try too hard to stick to rules like "1 table = 1 model" and "User::getAllFoos + controller processing > Report::getMonthlyOrderStats()".

Categories