Laravel mysqli connect 2 columns and change column name - laravel

I have 2 tables that I want to connect
table1:
id
name
table2_id
table_2:
id
name
table1 is connected with table2
When I try to select table1 and the connected row from table2 it works, but because the row names id and name are the same, it overwrites the values of table1.
How can I connect table1 with table2 and get a table1 with this rows in laravel:
id
name
table2_id
table2_name

Review the laravel documentation regarding relationships - If you set up your models as outlined here https://laravel.com/docs/5.3/eloquent-relationships you will be able to call the table2 like:
$result = Table1::find(1)->table2;
However to get the result you want you can use the ->select( function
DB::table('users')->select('name', 'email as user_email')->get();
https://laravel.com/docs/5.2/queries#selects
Something like this
public function scopetable1($query,$id){
return $query = DB::table('table1')
->select('table1.id as id','table1.name as name','table2.id as table2_id', 'table2.name as table2_name' )
->where('table1.id', '=', $id)
->leftJoin('table2', 'table1.table2_id', '=', 'table2.id')
->get();
}

Related

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)

Using same table in subquery

I am failing to convert next SQL code into laravel eloquent:
SELECT t1.template, t1.created_at
FROM sent_emails t1
where created_at = (
select max(created_at) from sent_emails t2 where t2.template = t1.template
)
group by t1.created_at, t1.template
or:
SELECT t1.template, t1.created_at
FROM sent_emails t1
JOIN
(
SELECT Max(created_at) date, template
FROM sent_emails
GROUP BY template
) AS t2
ON t1.template = t2.template
AND t1.created_at = t2.date
group by t1.created_at, t1.template
Both queries return same data set. Creating subquery in separate variable is not an option as I need multiple values to be returned from it.
I also don't know how can I set alias name if I create table using models (and not using DB::), so this is my unsuccessful try:
$sent_emails = SentEmail::where('created_at', function($query) {
SentEmail::where('template', 't1.template')->orderBy('created_at', 'desc');
})->groupBy('template', 'created_at')->get(['template', 'created_at']);
You query should be something like this (I'm not at a computer to test this, so it may require further editing)
$sent_emails = SentEmail::where('created_at', function($query) {
$query->where('created_at', SentEmail::->whereColumn('column_name', 'table.column_name')->orderBy('created_at', 'desc')->max('created_at'));
})->groupBy('template', 'created_at')->get(['template', 'created_at']);

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.

Crudbooster laravel - How to make different column relationship?

I have two table 1. table_a 2. table_b
And all table have company_id. I want to make relationship among this two table using same company id . Like
select * from table_a left join table_b on table_a.company_id = table_b.company_id;
Please help me out if its really possible. Thank you in advance.
You can use this:
$data = DB::table('table_a')->leftJoin('table_b', 'table_a.company_id', '=', 'table_b.company_id')->get();
Or raw:
$users = DB::table('table_a')
->select(DB::raw('query here'))
->get();
More on:
https://laravel.com/docs/5.7/queries

Wherein query to related tables

I have been trying to a query where in I want to do some validation on the related tables. I would like to do something like this using Laravel 5
select
*
from
table1
INNER JOIN table3 on(table1.id = table3.table1_id)
INNER JOIN table2 on(table1.id = table2.table1_id)
where
table2.column2 in ('arr1', 'arr2');
Also table1 is related to 5 - 7 tables and I want to eager load all of this. Here's what I had so far
$reports = Table1::with(
[
'table3',
'table4',
'table5.table6',
'table7',
'table8',
])
->where('created_at', '>=', date('Y-m-d', strtotime($request->get('from'))))
->where('created_at', '<', date('Y-m-d', strtotime($request->get('to'))))
->with('table2',function($query) use($table1_column){
return $query->whereIn('table1_column',$table1_column);
});
But this displays everything. Even the items that does not exist in table2. What I would like to achieve is to create a result where all items is the only items that exists in table2. Meaning all transaction made using items in table2.
Assuming the item in table2 has an ids of 123, 456, and 789 then I would like to display all record related to this id's
How can I make this kind of result
You can use the whereHas method.
$reports = Table1::with(
[
'table3',
'table4',
'table5.table6',
'table7',
'table8',
])
->whereHas('table2', function($query) use($table1_column){
return $query->whereIn('table1_column',$table1_column);
})
->where('created_at', '>=', date('Y-m-d', strtotime($request->get('from'))))
->where('created_at', '<', date('Y-m-d', strtotime($request->get('to'))));
Note that this does not include the table 2 data in the result set. If you need that include the table2 relation name in the with method call.

Resources