How to use orWhere to create dynamic where clause - Laravel 4 - php

how I can to write this in Eloquent and I'm uisng laravel 4?
SELECT * FROM `applicant` WHERE `id`='17' OR `id`='19'
I am thinking like this
$findIDAUTH = Auth::User()->id;
$findHrsbu = HrSbu::where('staff_id', '=', $findIDAUTH)->get();
$results = Mapplication::MApplication();
foreach($findHrsbu as $HrSbu){
$results = $results->Where('sbu.id', '=', $HrSbu->sbu_id);
}
$results = $results->get();
if just
SELECT * FROM `applicant` WHERE `id`='17'
i'ts work but if
SELECT * FROM `applicant` WHERE `id`='17' OR `id`='19' or 'id'='20'
not working for me thanks

You should use whereIn with a subquery:
$results = Mapplication::whereIn('id', function ($query) {
$query->from((new HrSbu)->getTable())
->select('sbu_id')
->where('staff_id', auth()->id());
})->get();

Hi Please try with this code and let me know, here is an option by laravel as you used its a similar method....
$findIDAUTH = Auth::User()->id;
$findHrsbu = HrSbu::where('staff_id', '=', $findIDAUTH)->get();
$results = Mapplication::MApplication()->where(function($findHrsbu)->where(function($results) use ($findHrsbu) {
foreach($findHrsbu as $HrSbu){
$results->orWhere('sbu.id', '=', $HrSbu->sbu_id);
}
})->get();

Hy guys thanks for your answer..
Problem solved I'm using WhereIn
for ex : $results->whereIn('sbu.id', '=', array($dataArray));

Related

How Can We Use Or With Wherebetween Clause in eloquent laravel

$checking = DB::table('bookings')->whereBetween('checkindate', [$checkindate, $checkoutdate])->first();
------My first query--------
$checking_2 = DB::table('bookings')->where('checkindate',$checkindate)->where('checkoutdate',$checkoutdate)->first();
--------My Second query-------
$checking_3 = DB::table('bookings')->whereBetween('checkoutdate',[$checkindate,$checkoutdate])->first();
------ My third query-------------
I want to build these queries into a single query but unable to do it can anybody help it with me.
I want to run each query and store the result in a single array .
Main question how to use OR after wherebetween query because if I simply use ->Where it doesn't give me desire result
or can anywrite this code in a single query?
Try
$camps = DB::table('bookings')->where(function ($q)
{
$q->whereBetween('checkindate', [$checkindate, $checkoutdate])
->orWhere(function($q1) {
$q1->where('checkindate',$checkindate)->where('checkoutdate',$checkoutdate)
})
->orwhereBetween('checkoutdate', [$checkindate, $checkoutdate])
})->first();
Try this code.
$checking = DB::table('bookings')
->whereBetween('checkindate', [$checkindate, $checkoutdate])
->orWhereBetween('checkoutdate',[$checkindate,$checkoutdate])
->orWhere(function($query){
$query->where('checkindate',$checkindate)->where('checkoutdate',$checkoutdate)
})
->first();
You can use the following query to get the result.
->where(function($query) use($checkindate, $checkoutdate) {
$query->whereBetween('checkindate', [$checkindate, $checkoutdate])
->orWhere(function($q) use($checkindate, $checkoutdate) {
$q->where('checkindate',$checkindate)->where('checkoutdate',$checkoutdate)
})
->orWhereBetween('checkoutdate',[$checkindate,$checkoutdate])
})->first();
use this one
$checking_2 = DB::table('bookings')->where('checkindate','>=' ,$checkindate)->where('checkoutdate','<=',$checkoutdate)->get();

How to fix whereNotExists and where query builder laravel

I try to combine whereNotExists and where, if you use it simultaneously it will and if you try one of them you can
$query = DB::table('tabel_produk')->where('kode_customer',$cust)->whereNotExists(function($query){
$query->select(DB::raw(1))->from('tabel_detail_realisasi')->whereRaw('tabel_detail_realisasi.barcode = tabel_produk.barcode');
})->distinct()->orderBy('barcode','asc') ->get();
Pleae help me with this
I don't have your DB schema so I've tried something similar with a local DB I have and it seems to be working fine. See my example below. Could you please replace my query with yours and see what's output by the query logger?
DB::flushQueryLog();
DB::enableQueryLog();
$results = DB::table('product_templates')
//Non deleted products
->where('deleted', '=', '0')
//
->whereNotExists(function ($query) {
//Where the category is not 'General'
$query
->select(DB::raw(1))
->from('product_categories')
->whereRaw('product_categories.id = product_templates.category_id')
->where('product_categories.name', 'General');
})
->distinct()
->orderBy('name', 'asc')->get();
DB::disableQueryLog();
$queryLog = DB::getQueryLog();
logger("Query results", compact('results', 'queryLog'));

Laravel 5.4 joining two tables

I'm quite lost here. I am trying to get the list of people who are on the Records table which aren't on the Profiles tables which has the specific course (e.g Psychology) and year (e.g 2011). So far, this is what I came up with and unfortunately it's not working.
$check = Record::join('Profiles', function ($join) {
$join->on('Records.firstname', '!=', 'Profiles.firstname');
$join->on('Records.lastname', '!=', 'Profiles.lastname');
$join->on('Records.middlename', '!=', 'Profiles.middlename');
})
->select('Records.*', 'Profiles.*')
->where('Records.year', '2011')
->where('Records.course', 'Psychology')
->get();
dump($check);
Is there any way that I can go around about this? I'm new to this. Thanks in advance. Advises and tips on joining tables would be greatly appreciated.
Please try a NOT EXISTS subquery:
use Illuminate\Database\Query\Builder;
$check = \DB::table('Records')
->whereNotExists(function(Builder $query) {
$query->select(\DB::raw(1))
->from('Profiles')
->whereRaw('Records.firstname = Profiles.firstname')
->whereRaw('Records.middlename = Profiles.middlename')
->whereRaw('Records.lastname = Profiles.lastname');
})
->select('firstname', 'middlename', 'lastname')
->where('year', 2011)
->where('course', 'Psychology');
->get();
I would look at doing a left outer join and then selecting the records that have a null profile name.
$check = Record::leftJoin('Profiles', function ($join) {
$join->on('Records.firstname', '=', 'Profiles.firstname');
$join->on('Records.lastname', '=', 'Profiles.lastname');
$join->on('Records.middlename', '=', 'Profiles.middlename');
})
->select('Records.*', 'Profiles.*')
->where('Records.year', '2011')
->where('Records.course', 'Psychology')
->whereNull('Profiles.firstname')
->get();

Running “exists” queries in Laravel query builder

I'm trying to execute this :
SELECT * FROM `lms_test`
WHERE NOT EXISTS
(SELECT * FROM `lms_studenttest`
WHERE `lms_test`.slug = `lms_studenttest`.testId
AND `lms_studenttest`.`studentId`='10a75c804b8851520993dedc42334c0f'
)
AND `lms_test`.`testType`= 'Practice Test'
But not getting success.
Help me to do this.
I think you can try this example :
A::whereNotExists(function($query){
$query->select(DB::raw(1))
->from('B')
->whereRaw('A.id = B.id');
})
->get();
OR
DB::table('lms_test')
->whereNotExists(function ($query) {
$query->select('lms_studenttest.*')
->from('lms_studenttest')
->where('lms_test.testType', 'Practice Test')
->where('lms_studenttest.studentId', '10a75c804b8851520993dedc42334c0f')
->whereRaw('lms_studenttest.testId = lms_test.slug');
})
->get();
Hope this help for you !!!
Try:
DB::table('lms_test')
->whereNotExists(function ($query) {
$query->select(DB::raw(1))
->from('lms_studenttest')
->where('lms_test.testType', 'Practice Test')
->where('lms_studenttest.studentId', '10a75c804b8851520993dedc42334c0f')
->whereRaw('lms_studenttest.testId = lms_test.slug');
})
->where('testType','Practice Test')
->get();
try this (I don't check it on real data , I wrote only logic):
SELECT *
FROM `lms_test`
LEFT OUTER JOIN `lms_studenttest`
ON `lms_test`.slug = lms_studenttest`.testId
WHERE `lms_studenttest`.testId IS NULL
AND `lms_studenttest`.`studentId`='10a75c804b8851520993dedc42334c0f'
AND `lms_test`.`testType`= 'Practice Test'
Try the following conditional query and let me know if its help:
if (User::where('email', '=', Input::get('email'))->exists()) {
// user found
}

Query building using Kohana Query Builder

If I got this:
$result =
DB::select(
'orders.date_created'
)
->from('orders')
->execute()->as_array();
For normal, dsiplaying all orders date_created. Now I can filter by user, doing this ?user_id=112
This will make me having this:
if(isset($get['user_id']))
{
$result =
DB::select(
'orders.date_created'
)
->from('orders')
->where('user_id', '=', $get['user_id'])
->execute()->as_array();
}else{
$result =
DB::select(
'orders.date_created'
)
->from('orders')
->execute()->as_array();
}
Although this is no problem for me have this, but this get to be real much ugly code when I have 4 params to filter out from(4 where statements) e.g ?user_id=112&quantity=2&...
Can I somehow make a inline if/action statement, which would grab all user_ids if there's nothing in $get['user_id'] ?
So I can end up having only this:
$result =
DB::select(
'orders.date_created'
)
->from('orders')
->where('user_id', '=', (isset($get['user_id']) ? $get['user_id'] : 'GRAB ALL!'))
->execute()->as_array();
or is there another, even better way to do this?
Tried looking in Kohana docs if there's some ->if()->where()->exitif().. (just some imagination of how it could work)
You can modify Query object before executing:
$query = DB::select()->from(...);
if (isset($get['user_id'])) {
$query->where('user_id', '=', intval($get['user_id']));
}
if (isset($get['quantity'])) {
$query->where('quantity', '=', floatval($get['quantity']));
}
$result = $query->execute()->as_array();

Categories