Laravel retrieve data from different tables and show in blade - laravel

I have 2 table A and B
A: id, b_id (which is the value of B's id & share many query rows), name, balance
B: id, country, state, age
In my view, I want to retrieve the data of A which has some specific id from B
Like: #foreach ($B_TABLE->id as $b_id)
{{ $b_id->sum('balance') }} <--- In this I want to retrieve the total sum of every 'balance' column from A table which has the id of $b_data in b_id column
What should be done in the Model/Controller to achieve that?

Try this Eloquent query with join:
$query = TableB::join('table_a', 'table_a.table_two_id', '=', 'table_b.id')
->select('table_b.id', \DB::raw("SUM(table_a.balance) as total_balance"))
->where('table_a.table_two_id', $id)
->groupBy('table_a.table_two_id')
->get();

Related

Laravel. Join three tables from three way pivot where one column (jsonb) contains an array of id's

I have a table called user_business_survey which has a user_uuid, business_uuid and a third containing one or more survey_uuid's IN A JSONB column. I need to query the DB to bring back the following two scenarios: USER1 belongs to BUSINESS1 AND HAS SURVEY A, SURVEY B, SURVEY C then also USER1 belongs to BUSINESS3 AND HAS SURVEY B, SURVEY D, SURVEY E
This is the query I am trying but to no avail. It return empty.
DB::table('user_business_survey')
->select(['*'])
->leftJoin('businesses', 'businesses.uuid', '=', 'user_business_survey.business_uuid')
->leftJoin('surveys', 'surveys.uuid', '=', 'user_business_survey.survey_uuid')
->whereJsonContains('survey_uuid', 'surveys.uuid')
->where('user_business_survey.user_uuid', '=', $id)
->get();
DB table is as follows:
user_uuid
business_uuid
surveys_uuids is an column with one or more survey id's as jsonb
I need to itrate through all id's and join them from the jsonb column
I thank you in advance for any help to build this query

How to solve Laravel union queries with different number of columns

I have two tables contact_us and upload_new_car.
contact_us table contains columns:
id
name
email
phone
message
created_at
updated_at
upload_new_car contains columns:
id
name
phone_number
car_name
car_price
location
car_model_year
car_model
variant
driven
fuel
transmission
city
no_of_owners
upload_1
upload_2
upload_3
upload_4
upload_5
created_at
updated_at
How can I get the UNION of these tables in Laravel? Please help
Not allowing to different size of columns is not laravel's businuess. It is a matter of SQL .You can follow [this link] for more info about UNION statements .
On the other hand for laravel you can use those syntax using union (We can benefit from selecting the same count of columns from each tables).
$first = DB::table('contact_us')
->select('name','phone');
$users = DB::table('users')
->select('name','phone_number as phone')
->union($first)
->get();
dd($users)

Laravel: How to get the latest record in group of records with where condition?

I have two tables which are named as students and cities. students table has a primary key which is related to the cities table.
Students table:
In the frontend, I would like to have the data of the cities table with these requirements:
The data of the students table must be grouped by city_id
If there are more than one records with the same city_id in the students table, select only the latest record of the group.
Search in the latest records and select only students who are inactive.
Here is the relationship function of the city model:
public function student()
{
return $this->hasOne(Student::class, 'city_id', 'id')->orderByDesc('id');
}
This is my controller function:
$data = City::whereHas('student', function($query){
$query->where('is_active', 0)
})->with('student')->get();
Expected result: Considering the sample data, the query must return nothing.
Current result: It returns the third row as there is an inactive student record in the second row. So in this case where condition doesn't work properly.
I can get expected result with this SQL query:
select *
from students s
where id = (select max(t2.id)
from students s2
where s.city_id = s2.city_id) AND is_active = '0';
How can I fix this logical error?
What about something like this?
public function inactiveStudent()
{
return $this->hasOne(Student::class, 'city_id', 'id')
->where('is_active', 0)
->orderByDesc('id');
}
$data = City::whereHas('inactive_student')
->with('inactive_student')
->get();
I'm not certain if it's inactive_student or inactiveStudent when you do the query.

row count from second table with more than one table join

I have three table:
TABLE1:
jobposts (id,user_id,qualification_id)
TABLE2:
appliedjobs (id,jobposts_id,message)
TABLE3:
qulificationmasters (id, qualificationame)
jobposts_id from TABLE is foreign key of id of jobposts
I need all records from jobposts with row count of appliedjobs
I tried by this:
$jobposteddetails = DB::table('jobposts')
->rightJoin('qulificationmasters','qulificationmasters.id', '=', 'jobposts.qualification_id')
->leftJoin('appliedjobs','appliedjobs.jobposts_id', '=', 'jobposts.id')
->select('jobposts.*','qulificationmasters.QualificationName', DB::raw('count(appliedjobs.id) as counts'))
->where('jobposts.user_id', '=', $user->id)
->groupBy('appliedjobs.id')
->get();
But gives error like: it wants every column in groupby of jobposts and qulificationmasters and qulificationmasters.id
But gives error like: it wants every column in groupby of jobposts and qulificationmasters and qulificationmasters.id
If I groupby with all columns of jobposts and qulificationmasters then it is working good.
But why all the columns gropuby.
Go in config folder database.php find mysql array and make strict to false and check again.

how to retrieve multiple table data with multiple criteria in laravel eloquent?

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.

Resources