What is the correct way to get a count of rows whose specified column is a foreign key of another table. Here's what I mean:
I have 2 tables providers and products. The providers table has primary key provider_idand the table products has a foreign key reference to provider_id via a column named the same, provider_id. My intention is to fetch the details of each provider including all their products, that is a total of all instances where provider_id in the products table matched providers_id in the providers table.
Here's my code:
$data = DB::table('providers')
->join('products', 'products.provider_id', '=', 'providers.provider_id')
->select(
'providers.provider_id AS id',
'providers.provider_name AS name',
DB::raw("count(products->where('products.provider_id','=','providers.provider_id')) AS total_products"),
'providers.claims AS claims',
'providers.settlements AS settlements')
->groupBy('provider')
->get();
This seems not to work as I am getting the error below:
Undefined property: Illuminate\Database\MySqlConnection::$id (View: /var/www/test/app/resources/views/agent/serviceproviders/index.blade.php)
What am I doing wrong?
The issue was in how I was counting the rows and how I was grouping the data for proper counting. I feel so dumb, this was too easy. Below is the correct code:
$data = DB::table('providers')
->join('products', 'products.provider_id', '=', 'providers.provider_id')
->select(
'providers.provider_id AS id',
'providers.provider_name AS name',
DB::raw("count(products.provider_id) AS total_products"))
->groupBy('providers.provider_id')
->get();
Related
I have two tables users/invoices in 1:n relation. When someone creats an invoice it saves the user_id. Also it's possible (optional) to set a manager (column: project_management_user_id). If there is no manager set (NULL), I want to get the user, who created the record. So for now I have this query. It gives me all invoices with the manager (but not that ones with manager = null.
$query = Invoice::join('users', 'invoices.project_management_user_id', '=', 'users.id')
->select('users.name as name', 'users.color as color', DB::raw("count(invoices.id) as count"))
->get();
Is it possible to add some if-clause like "if project_management_user_id is null, take the user_id"? I read about the whereNull() function, but it filters my whole query
Use leftJoin instead of join in the query.
Grouping error: 7 ERROR: column "id" must appear in the GROUP BY clause or be used in an aggregate function
i want to show any id after I did the GroupBy multiple Column. even if the id is from first or from last
my code is :
$data = Book::select(
'id',
'tittle',
'writer',
)
->groupBy('tittle','writer',)
->paginate(10);
Remove Id column from select function.
$data = Book::select(
'tittle',
'writer',
)
->groupBy('tittle','writer',)
->paginate(10);
Beacuse grouping is done when we have a duplicate record in our tables but id is primary key so remove it and than try again hope it will work
I want to retrieve data from 4 different table using laravel eloquent with multiple criteria.
an Overview of my table;
table 1
id
name
table 2
id
name
year
table1_id
table 3
id
name
description
table2_id
table 4
id
name
quarter
table3_id
below are their relations
table 1
hasMany -> table 2
table 2
belongsTo ->table1
HasMany->table2
table 3
belongsTo ->table2
HasMany->table3
table 4
belongsTo ->table3
I'd like to fetch the data by resource show with two parameters
and i tried this
$Report = Table1::whereHas('tabke1', function($query){
$query->where('year', 'like','%'.$year.'%');
})
->with('table2')->whereHas('table3', function($query){
$query->where('quarter', 'like', '%'.$quarters.'%');
})
->get();
but im receiving syntax error.
How can I retrieve the data from multiple table with multiple filter?
i tried this table query to understand more what i expect
SELECT `table1`.*, `table2`.*, `table3`.*, `table4`.*
FROM `table1`, `table2`, `table3`, `table4`
WHERE ((`table2`.* year = 2019) AND (`table4`.* quarter = 1))
I reckon there are two queries to achieve the results.
The first query is something like:
Table1::whereHas('table2', function ($query) {
$query->where('year', 2019);
})
->whereHas('table2.table3.table4', function ($query) {
$query->where('quarter', 1);
})
->get();
The second query is something like:
Table1::select('table1.*')
->join('table2', 'table1.id', '=', 'table2.table1_id')
->join('table3', 'table2.id', '=', 'table3.table2_id')
->join('table4', 'table3.id', '=', 'table4.table3_id')
->where('table2.year', 2019)
->where('table4.quarter', 1)
->distinct()
->get();
Regarding performance, I prefer the second query.
I have two table application_requests and transactions in both the tables there may be matching record or may not be. For any condition i want record from both the tables.
In transactions table there is a foreign key column application_request_id (this has value of reference of primary key in application_requests table).. If this condition matched then it should display as one row (or record).
I dont know how to achieve this in laravel.
I have tried below codes but its not working:
$a = \DB::table('application_requests')->select('id');
$b = \DB::table('transactions')->select('application_request_id');
$results = $b->union($a)->get();
echo "<pre>";
print_r($results);die;
transactions table is
And my application_requests table is
$results = DB::table('transactions')
->leftJoin('application_requests','transactions.application_request_id','=','application_requests.id')
->select('transactions.partner_id as tr_pratnerid','transactions.application_request_id as tr_applicationrequestid','transactions.class_2_1 as tr_clas21','transactions.class_2_2 as tr_clas22','transactions.class_2_3 as tr_clas23','transactions.class_3_1 as tr_clas31',
DO THIS TO ALL FIELDS FROM BOTH TABLE)
->get();
echo "<pre>";
print_r($results);die;
Two outer joins to show all rows from both tables:
$second = DB::table('t2')
->rightJoin('t1', 't1.id', '=', 't2.id')
$first = DB::table('t1')
->leftJoin('t2', 't1.id', '=', 't2.id')
->unionAll($second)
->get();
Source: https://stackoverflow.com/a/41662283/4587214
just like that:
DB::table('transactions')->join('application_requests', 'transactions.application_request_id', '=', 'application_requests.id', 'full outer');
I have the following working for laravel paginate
$query = DB::table('clients')
->leftJoin('radusergroup', 'clients.username', '=', 'radusergroup.username')
->leftJoin('recharge', 'clients.username', '=', 'recharge.soldto');
I want to join some values from two tables radusergroup and recharge. radusergroup always return one row as it has only one row stored whereas recharge table return multiple rows. I want only one row returned from recharge table which is latest entry.
Right now its return all the possible rows from recharge table and showing it on paginated view.
This is Laravel Official documentation
DB::table('clinets')
->leftJoin('radusergroup', 'clients.username', '=', 'radusergroup.username')
->leftJoin('recharge', function ($leftJoin) {
$leftJoin->on('clients.username', '=', 'recharge.soldto')
->where('recharge.create_date', '=', DB::raw("(select max(`create_date`) from recharge)"));
})
->get();
this is the case if you have create_date column in your table, if you haven't got it I strongly recommend to create that column.