Full outer join query in laravel 5.4 - laravel

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

Related

Laravel Eloquent join if column null

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.

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

Getting Count of Rows With Join in Laravel

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

left join in laravel return null if data exist in database

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

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