left join in laravel return null if data exist in database - laravel

I'm trying to left join two tables in laravel. after joining got all second table value is empty but data exist in a database .

Can you try this:
$contacts = DB::table('C_Contacts')
->leftJoin('C_Contact_Category_Map', 'C_Contacts.contact_id', '=', 'C_Contact_Category_Map.contact_id')
->get();

Can You try this
$contacts = DB::table('C_Contacts')
->join('C_Contact_Category_Map', 'C_Contacts.contact_id', '=', 'C_Contact_Category_Map.contact_id')
->get();

Related

Join 2 Temp tables laravel

I have following temp tables I wanna join them In query builder but I am failing to do so. I wanna join these 2 tables so I can do i1.imp/i2.imp
$subQuery1 = MyModel::query()
->from("table as i1")
->select(
\DB::raw('sum(col) as col1'),
\DB::raw('co1')
)->where('stamp', '>=', '2022-03-01 14:25:00')
->where('stamp', '<', '2022-03-07 14:30:00')
->groupBy(
'co1'
);
$subQuery2 = MyModel::query()
->from("table as i2")
->select(
\DB::raw('sum(col) as col1'),
\DB::raw('co1')
)->where('stamp', '>=', '2022-03-01 14:20:00')
->where('stamp', '<', '2022-03-07 14:25:00')
->groupBy(
'co1'
);
You can use from, fromSub or table and pass in a subquery instead of a table.
You can do the same with joinSub for joins.
You need to provide an alias though.
For example, to use $subquery1 as the main table and join it with $subquery2, the resulting query could look like this:
$results = DB::query()
->select(.....)
->fromSub($subquery1, 'i1')
->joinSub($subquery2, 'i2', function ($join) {
$join->on('i1.col', '=', 'i2.col');
// ->orOn(....)
});
->where(....)
->get();
Laravel 9.x API - fromSub
Queries - Subquery Joins

how to fetch data from 3 different table in laravel using advance join

I have 3 table
This is my first table cart_items .👇🏻
This is my second table ecomm_products.👇🏻
This is my Third table ecomm_sellers.👇🏻
$result = DB::table('cart_items')
->join('ecomm_products','ecomm_sellers',function($join){
$join->on('ecomm_products.id','=','cart_items.product_id')->where('cart_items.order_id','=',1);
$join->on('ecomm_products.seller_id','=','ecomm_sellers.id');
})
->where('cart_items.user_id','=',Auth::user()->id)
->select('ecomm_products.product_name','ecomm_products.showcase_image','ecomm_products.selling_price','cart_items.quantity','cart_items.id')
->get();
But I am getting an error ⛔⛔⛔
strtolower(): Argument #1 ($string) must be of type string, Closure given
When I have not included 3 tables (ecomm_sellers) till the query is running good but when I add it sends me some errors.
The question is: Why I am getting this error and why this query is not working, What else I can add so that my query working fine.
You cannot join two tables in by passing the table names as arguments to join. If you check the API documentation of the join method you can see that its signature is:
$this join(string $table, Closure|string $first, string|null $operator = null, string|null $second = null, string $type = 'inner', bool $where = false)
so you can only join two tables at a time and if you want to join more than two tables you chain another call to the join method.
try this:
$result = DB::table('cart_items')
->join('ecomm_products', 'ecomm_products.id', '=', 'cart_items.product_id')
->join('ecomm_sellers' , 'ecomm_products.seller_id', '=', 'ecomm_sellers.id')
->where('cart_items.order_id', '=', 1)
->where('cart_items.user_id', '=', Auth::user()->id)
->select([
'ecomm_products.product_name',
'ecomm_products.showcase_image',
'ecomm_products.selling_price',
'cart_items.quantity',
'cart_items.id'
])
->get();
You can find examples for Laravel query builder joins here

how to join query different table in laravel

I try to join three tables which are named attendances, categories, and users.
my code is
$Attendance= DB::table('users')
->join('categories','users.U_category_id', '=', 'categories.id')
->select('users.*','categories.batch_name')->get();
but I also need to join attendances table where attendances column User_A_ID and users table id are the same. How can I do this?
You can try it with
$Attendance= DB::table('users')
->join('categories','users.U_category_id', '=', 'categories.id')
->join('attendances','users.id', '=', 'attendances.User_A_ID')
->select('users.*','categories.batch_name')->get();

Laravel Left Join Query

I am using laravel 5.3 and I have some left join query with error in laravel query method.
This is my normal query
SELECT bran.branchName,sch.schoolName From m_schoolbranch bran
LEFT JOIN m_students stu ON stu.schoolNo=bran.schoolNo AND stu.branchNo=bran.branchNo
LEFT JOIN m_school sch ON sch.schoolNo=stu.schoolNo where stu.userNo='0000000001';
And this is my new laravel Query
DB::table('m_schoolbranch')
->join('m_students', 'm_schoolbranch.schoolNo', '=', 'm_students.schoolNo')
->join('m_students', 'm_schoolbranch.branchNo', '=', 'm_students.branchNo')
->join('m_school', 'm_schoolbranch.schoolNo', '=', 'm_school.schoolNo')
->select('m_school.schoolName', 'm_schoolbranch.branchName')
->where('m_students.userNo',$userNo)
->get();
In these query I need to match two column in table m_students so I put like this
->join('m_students', 'm_schoolbranch.branchNo', '=', 'm_students.branchNo')
But i show error...
Tables in the query need to have unique names, otherwise the DB has no way of knowing which m_schoolbranch should be used when evaluating m_schoolbranch.schoolNo.
You could use unique table aliases in your join statements but I recommend using multiple conditions on the join. Just like you use in your original SQL query. See here: https://stackoverflow.com/a/20732468/4437888
DB::table('m_schoolbranch')
->join('m_students', function($join)
{
$join->on('m_schoolbranch.schoolNo', '=', 'm_students.schoolNo');
$join->on('m_schoolbranch.branchNo', '=', 'm_students.branchNo');
})
->join('m_school', 'm_schoolbranch.schoolNo', '=', 'm_school.schoolNo')
->select('m_school.schoolName', 'm_schoolbranch.branchName')
->where('m_students.userNo',$userNo)
->get();

Laravel join with limit

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.

Resources