Running “exists” queries in Laravel query builder - php

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
}

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

Inner query with use variable

I am new to laravel and I am trying to retrieve data from three tables because the user enters 3 information then I use its to determine the row to check login
so I use this way but not work!
I do not know how to send variables in SQL
DB::table('members')
->join(DB::raw('(SELECT FROM members_courses_assign WHERE referenceNumber=>$coursenum,termkey=>$semester) courseA'), function($join) {
$join->on('members.externalPersonKey', '=', 'courseA.externalPersonKey');
})->join(DB::raw('(SELECT courses FROM WHERE referenceNumber=>$coursenum,termkey=>$semester) coursecc '), function($join) {
$join->on('courseA.referenceNumber', '=', 'coursecc.referenceNumber');
})
->where(['jobID' => $jobid])->get();
Try this :
->where('jobID',$jobid)->get();
Here in Laravel Documentation you can find all you need to know about query builder.
To pass variable to inner query (Anonymous functions) you have to pass it with use keyword as described in docs
$yourVariable= "";
DB::table('members')
->join(DB::raw('(SELECT FROM members_courses_assign WHERE referenceNumber=>$coursenum,termkey=>$semester) courseA'), function($join) use($yourVariable) {
$join->on('members.externalPersonKey', '=', 'courseA.externalPersonKey');
})->join(DB::raw('(SELECT courses FROM WHERE referenceNumber=>$coursenum,termkey=>$semester) coursecc '), function($join) use($yourVariable) {
$join->on('courseA.referenceNumber', '=', 'coursecc.referenceNumber');
})
->where(['jobID' => $jobid])->get();
DB::table('members')
->join('members_courses_assign', function($join) use ($coursenum, $semester) {
$join->on('members.externalPersonKey', '=', 'members_courses_assign.externalPersonKey')
->where('referenceNumber', $coursenum)
->where('termkey', $semester);
})
->join('courses', function($join) use ($coursenum, $semester) {
$join->on('members_courses_assign.referenceNumber', '=', 'courses.referenceNumber')
->where('referenceNumber', $coursenum)
->where('termkey', $semester);
})
->where('jobID', $jobid)
->get();
You can refer to Query Builder's advanced join clauses here.

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

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

Categories