How Can We Use Or With Wherebetween Clause in eloquent laravel - php

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

Related

Convert SQL to Laravel ORM RELATONS

It is working but I should do with Relaion
SELECT * FROM product JOIN ware_houses ON product.id=ware_houses.product_id WHERE ware_houses.variant_id=1;
Like this...
Product::whereHas('ware_houses')->where('ware_houses.product_id',1)->get();
But it is not working because whereHas returns a collection, What I can do ??? Please give advice.
You need to use whereHas() to actually filter:
Product::whereHas('ware_houses', function($q) {
$q->where('ware_houses.product_id',1);
})->get();
This is working thank you !!!
$products=Product::whereHas('wareHouse',function(Builder $query) use ($id){
$query->where('ware_houses.variant_id',$id);
})->get();

How to count results from database?

I want to count the result fetched from database
$t = Activation::where('user_id', '=', $user->id)->first();
$w=(count($t));
dd($w);
I expect to see the number of results fetched
Your code is wrong... Please read the Laravel official documentation
With first() function you're getting just the first result of the set returned from your query. To make your code work you should use the get() function. and the dd($w) will return the correct result.
Anyway there are specific aggregate functions to achieve your goal, just changing your code from
Activation::where('user_id', '=', $user->id)->first();
// output: {"user_id": 1, "email": 'test#example.com', [...]}
to
Activation::where('user_id', '=', $user->id)->count();
// output: 123
Try to use a laravel count method.
$t = Activation::where('user_id', '=', $user->id)->count();
dd($t);
$ty = Activation::where('user_id', '=', $user->id)->count();
dd($ty);
You are using first() that will return only one object if found. use get() and then use count.
$t = Activation::where('user_id',$user->id)->get();
$w = count($t);
If you just want to count then use count()
Activation::where('user_id',$user->id)->count();

Laravel retreive data from database order by creation date

I am trying to retrieve data orderby creation date desc and get the first one here is my code
$localnews = Articles::whereHas('sous_categories',
function ($query) {
$query->where('id', '15')->order_by('created_at', 'desc');
})->get()->first();
You must have got error when using order_by as its not the syntax in Laravel.
$localnews = Articles::whereHas('sous_categories',
function ($query) {
$query->where('id', '15')->orderBy('created_at', 'desc');
})->first();
Please have a look at official documentation to help you with.
And when using first() you don't need to use get().
get() we use to fetch multidimensional associative array.
first() we use to fetch one record which matches first.
Here is concise difference between all functions you will use then after. link.
It should be like this,
$localnews=Articles::whereHas('sous_categories', function($query) {
$query->where('id', '15')->orderBy('created_at', 'desc');
})->firstOrFail();

Collection WhereIn method

I'm trying filtered a collection, but the code in the last row does not work. In this row the $property->county_id is an integer, the params.county_id is an array. I would like know the array contains the integer.
I think the code is wrong, because the key (maybe) must be the params.county_id. How I can do this?
Thanks the answers.
$buyerSearches = collect($items);
$result = $buyerSearches
->where('params.type', '=', $property->type)
->where('params.sale_type', '=', $property->sale_type)
->whereIn('params.contract_type', ['all', $property->contract_type])
->where('params.min_price', '<=', $property->price)
->where('params.max_price', '>=', $property->price)
->whereIn($property->county_id, 'params.county_id');
Be sure that the second argument in the whereIn is an array or create that array before the $result query and use it next.
I solved, it works :)
$result = $buyerSearches
->where('params.type', '=', $property->type)
->where('params.sale_type', '=', $property->sale_type)
->whereIn('params.contract_type', ['all', $property->contract_type])
->where('params.min_price', '<=', $property->price)
->where('params.max_price', '>=', $property->price)
->filter(function($buyerSearch) use ($property) {
return in_array($property->county_id, $buyerSearch['params']['county_id']);
});
Try this code instead :
->whereIn('county_id', 'params.county_id');

laravel multiple where clauses within a loop

Pretty much I want the query to select all records of users that are 25 years old AND are either between 150-170cm OR 190-200cm.
I have this query written down below. However the problem is it keeps getting 25 year olds OR people who are 190-200cm instead of 25 year olds that are 150-170 OR 25 year olds that 190-200cm tall. How can I fix this? thanks
$heightarray=array(array(150,170),array(190,200));
$user->where('age',25);
for($i=0;$i<count($heightarray);i++){
if($i==0){
$user->whereBetween('height',$heightarray[$i])
}else{
$user->orWhereBetween('height',$heightarray[$i])
}
}
$user->get();
Edit: I tried advanced wheres (http://laravel.com/docs/queries#advanced-wheres) and it doesn't work for me as I cannot pass the $heightarray parameter into the closure.
from laravel documentation
DB::table('users')
->where('name', '=', 'John')
->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
Like Jeemusu and the OP stated, you need to use advance wheres.
But if you want to pass a variable to the closure function you need to make use of the "use" approach:
$heightarray = array(array(150,170),array(190,200));
DB::table('users')
->where('name', '=', 'John')
->orWhere(function($query) use ($heightarray){
//...
})
->get();
I found the answer. I need to include "use" in the closure to pass my $heightarray variable in. Once $heightarray is in then laravel's advance wheres work.
$heightarray=array(array(150,170),array(190,200));
$user->where('age',25);
$userprofile->Where(function($query) use ($heightarray) {
for($i=0;$i<count($heightarray);i++){
if($i==0){
$user->whereBetween('height',$heightarray[$i])
}else{
$user->orWhereBetween('height',$heightarray[$i])
}
}
});
$user->get();
This is completely untested, but looking at the documentation for advance wheres, it would seem you want to try something like this:
DB::table('users')
->where('age',25)
->Where(function($query)
{
for($i=0;$i<count($heightarray);i++){
if($i==0){
$query->whereBetween('height', $heightarray[$i]);
}else{
$query->orWhereBetween('height', $heightarray[$i]);
}
}
})->get();

Categories