My table "companies" has some many columns including position_id, position_id1, position_id2. So I need in Laravel to generate AND condition with 2 ORs. So when I search for companies with any of position_ids=[1,2,3] I'd SQL query will be "...AND (c.position_id in (1,2,3) OR c.position_id1 in (1,2,3) OR c.position_id2 in (1,2,3))".
Here is my PHP code which doesn't work (different combinations):
$position_ids = [1,2,3];
$companies = $companies->whereIn(function ($q) use ($position_ids) {
$q->whereIn('c.position_id', $position_ids)
->orWhereIn('c.position_id1', $position_ids)
->orWhereIn('c.position_id2', $position_ids);
});
$companies = $companies->orWhereIn(function ($q) use ($position_ids) {
$q->orWhereIn('c.position_id', $position_ids)
->orWhereIn('c.position_id1', $position_ids)
->orWhereIn('c.position_id2', $position_ids);
});
$companies = $companies->whereIn(function ($q) use ($position_ids) {
$q->orWhereIn('c.position_id', $position_ids)
->orWhereIn('c.position_id1', $position_ids)
->orWhereIn('c.position_id2', $position_ids);
});
And all the combinations above are getting the same error:
I am getting: Too few arguments to function Illuminate\Database\Query\Builder::whereIn(), 1 passed in ...\app\Http\Controllers\CompanyController.php on line 621 and at least 2 expected.
How to fix it? Thanks.
whereIn() function needs two argument
$companies->whereIn('id', function ($q) use ($position_ids) {
$q->whereIn('c.position_id', $position_ids)
->orWhereIn('c.position_id1', $position_ids)
->orWhereIn('c.position_id2', $position_ids);
});
Related
I'm a beginner regarding Laravel Query Builder, can you guys help me convert this SQL to Query Builder
public function check_available_room(Request $request, $checkin_date)
{
$available_rooms = DB::SELECT("SELECT * FROM rooms WHERE id NOT IN (SELECT room_id FROM bookings WHERE '$checkin_date' BETWEEN checkin_date AND checkout_date)");
}
Book model
function rooms() {
return $this->belongsTo(Room::class, 'room_id');
}
Room model
public function bookings() {
return $this->hasMany(Booking::class);
}
Thank you so much
Here is a solution if you have your models and relations in place
$availableRooms = Room::whereDoesntHave('bookings', function($bookingQuery) use ($checkin_date) {
$bookingQuery->where('checkin_date', '<', $checkin_date)
->where('checkout_date', '>=', $checkin_date);
})->get();
$unavailable_room_ids = Booking::where('checkin_date', '<', $checkin_date)
->where('checkout_date', '>=', $checkin_date)
->pluck('room_id')->get();
$available_rooms = Room::whereNotIn('id', $unavailable_room_ids)->get();
In Laravel, I want to build a query like following.
There is a status column, value should be 0 or 1.
And there is another column, device_ids with array.
Now,in query, I want to apply different logic on device_ids, when status is 0 and status is 1.
Can someone suggest me best possible way.
$builder->where('status','0')->whereJsonDoesntContain('device_ids', $imei_id->id)
$builder->where('status','1')->whereJsonContains('device_ids', $imei_id->id)
How can I combine both ?
$all_applications = $builder->where('status', $model->status)
->when($status, function ($query) {
Log::error("One");
return $query->whereJsonContains('device_ids', 107); },
function($query) {
Log::error("Zero"); return $query
->whereJsonDoesntContain('device_ids', 107); });
If I use $model->status , it gives me error like , "Undefined variable: model"
You have to use when.
$result = YourModel::where('status', $status)
->when($status, function ($query) {
return $query->whereJsonContains('device_ids', $imei_id->id);
}, function ($query) {
return $query->whereJsonDoesntContain('device_ids', $imei_id->id);
});
I am working on a new project in Laravel 8 where I am allowed to use as little RAW SQL as possible.
For a number of situations I have to Concat 2 columns together (as it is shown below) and that is actually easy to do with Raw Sql. But I am not allowed to use Raw Sql for that.
BoekhoudingVerdichting::select('boekhouding_verdichting.id', DB::raw("CONCAT(boekhouding_verdichting.verdichtingnr,' - ',verdichting) AS verdichting, CONCAT(boekhouding_hoofdverdichting.hoofdverdichtingnr,' - ',hoofdverdichting) AS hoofdverdichting"))
->join('boekhouding_hoofdverdichting', function ($q) {
$q->on('boekhouding_hoofdverdichting.id', 'boekhouding_verdichting.id_hoofdverdichting');
})
->join('boekhouding_grootboek', function ($q) {
$q->on('boekhouding_grootboek.id_verdichting', 'boekhouding_verdichting.id');
})
->groupBy('boekhouding_verdichting.id')
->orderBy('boekhouding_verdichting.verdichtingnr')
->orderBy('boekhouding_verdichting.verdichting')
->get();
I got a tip that I first have to get the data from the database as a Collection (as it is shown below) and then put the Collection through a map-function and in the map-function I can Concat the columns and return them! I searched the internet but I couldn't find any soloution.
Maybe you have another solution?
I hope you can help me with it.
BoekhoudingVerdichting::select('boekhouding_verdichting.id', 'boekhouding_verdichting.verdichtingnr', 'verdichting', 'boekhouding_hoofdverdichting.hoofdverdichtingnr', 'hoofdverdichting')
->join('boekhouding_hoofdverdichting', function ($q) {
$q->on('boekhouding_hoofdverdichting.id', 'boekhouding_verdichting.id_hoofdverdichting');
})
->join('boekhouding_grootboek', function ($q) {
$q->on('boekhouding_grootboek.id_verdichting', 'boekhouding_verdichting.id');
})
->groupBy('boekhouding_verdichting.id')
->orderBy('boekhouding_verdichting.verdichtingnr')
->orderBy('boekhouding_verdichting.verdichting')
->get();
Solved the problem!
$collection = BoekhoudingVerdichting::select('boekhouding_verdichting.id', 'boekhouding_verdichting.verdichtingnr', 'verdichting', 'boekhouding_hoofdverdichting.hoofdverdichtingnr', 'hoofdverdichting')
->join('boekhouding_hoofdverdichting', function ($q) {
$q->on('boekhouding_hoofdverdichting.id', 'boekhouding_verdichting.id_hoofdverdichting');
})
->join('boekhouding_grootboek', function ($q) {
$q->on('boekhouding_grootboek.id_verdichting', 'boekhouding_verdichting.id');
})
->groupBy('boekhouding_verdichting.id')
->orderBy('boekhouding_verdichting.verdichtingnr')
->orderBy('boekhouding_verdichting.verdichting')
->get();
$newCollection = $collection->map(function ($item, $key) {
$item['verdichting'] = $item['verdichtingnr'] . ' - ' . $item['verdichting'];
$item['hoofdverdichting'] = $item['hoofdverdichtingnr'] . ' - ' . $item['hoofdverdichting'];
return $item;
});
$solved = $newCollection->all();
in laravel 5.5 whereHas() is not working on where value is NULL. my relationship with other model is one to many and and i want to pick the values from the model where value is equal to NULL . But instead of returning the specific values its returning all the value in result
plans = Plan::with('objectives')->with('objectives.keyResults')
->whereHas('objectives', function($query) {
$query->whereNull('entity_id');
$query->whereNull('quarter_id');
})->where('companyKey', Auth::user()->companyKey)->get();
You have to specify the constraint twice:
$plans = Plan::with(['objectives' => function($query) {
$query->whereNull('entity_id');
$query->whereNull('quarter_id');
}, 'objectives.keyResults'])
->whereHas('objectives', function($query) {
$query->whereNull('entity_id');
$query->whereNull('quarter_id');
})->where('companyKey', Auth::user()->companyKey)
->get();
No double constraints are needed, simplify it to:
$plans = Plan::whereHas('objectives', function($query) {
$query->whereNull('entity_id');
$query->whereNull('quarter_id');
})->with(['objectives'])
->where('companyKey', Auth::user()->companyKey)
->get();
This is my code looks like right now :
$result = Category::with(array(
"getProducts",
"getProducts.getBrand",
"getProducts.getImages",
"getProducts.getDetail",
"getProducts.getTax",
"getProducts.getDiscount",
"getProducts.getAttributes" => function($query) use($_temp){
if($_temp != null){
$query->where_in('attributes.id',$_temp);
}
},
"getSlideshow",
"getSlideshow.getItems",
"getAttributeListing",
"getAttributeListing.getAttributes",
"getAttributeListing.getAttributes.productSpecific"
))
->where('id','=', $category_id)
->first();
Yet, It only filters the getProducts.getAttributes items not the 'getProducts' itself. Is there a way that I can get the Products by attributes?
Note: I am using Laravel 3