Laravel Collection Query, Where I am going wrong? - php

My tables looks like this
area_trip
|id|dispatch_id|trip_id|status|
equipment_trip
|equipment_id|trips_id|dispatch_id|
trips
|id|dispatch_id|status
I am trying to pass collection to my resource. Can someone check my query and tell me what I am doing wrong as following query returning all the data matches dispatch_id whether it matches equipment_id or not. Btw I am new to laravel.
return
Resources::collection(
area_trip::where('dispatch_id', $request->dispatch_id)
->where('status', 1)
->orWhere('status', 9)
->whereHas('equipment_trip', function($query) use ($request) {
$query->where('equipment_trip.equipment_id', '=', $request->equipment_id);
})
->with(['equipment_trip', 'createdBy', 'updatedBy', 'area', 'trips'])
->orderBy('tripStartDate', 'ASC')
->orderBy('status', 'ASC')
->get());
Here is the relationship set up in area_trip model
public function equipment_trip()
{
return $this->belongsTo(equipment_trip::class, 'trip_id', 'trips_id');
}

I believe your whereHas sub query is incorrect also instead of where and orWhere use where in and you can define all statuses necessary, try this:
Resource::collection(area_trip::where('dispatch_id', $request>dispatch_id)
->whereIn('status', [1, 9])
->whereHas('equipment_trip', function($query) use ($request) {
return $query->where('equipment_id', '=', $request->equipment_id);
})
->with(['equipment_trip', 'createdBy', 'updatedBy', 'area', 'trips'])
->orderBy('tripStartDate', 'ASC')
->orderBy('status', 'ASC')
->get());

Related

Can not use where clause after using with clause in Laravel query

I am writing a query using with and then where clause in laravel.
$users = User::query()->with('roles')->where('name', '!=', 'customer')->get();
return $users;
But where clause is not working here. Customers are not excluded. I am providing the snap shot of the query result.
I think you need whereHas() :
use Illuminate\Database\Eloquent\Builder;
$users = User::query()
->with('roles')
->whereHas('roles', function (Builder $query) {
$query->where('name', '!=', 'customer');
})
->get();
return $users;
I suppose you trying to filter relation data in base query.
Try smth like that:
$users = User::query()->with([
'roles' => function ($q) {
$q->where('name', '!=', 'customer');
}])
->get();
return $users;
Doc

Laravel with() and search using LIKE not working

So I have 2 tables, 1 table for storing debt(id, amount, category_id) and 1 table for storing debt categories(id, name). I am trying to pull the data for each month from the debt table, but I also have a search which seems to not work, I guess I am missing something.
I have the following:
$debt = $this->debtModel
->select(DB::raw('MONTH(created_at) as month'), DB::raw('SUM(amount) as amount'), 'category_id')
->where('user_id', Auth::user()->id)
->whereYear('created_at', $year)
->with(['category' => function ($query) use ($filter) {
$query->where('name', 'like', "%$filter%");
}])
->orderBy('month', 'asc')
->groupBy('month')
->groupBy('category_id')
->get();
Debt Model:
public function category()
{
return $this->hasOne('App\Models\DebtCategory', 'id', 'category_id');
}
This works fine, with the exception of search, If I try to filter by a category name it still returns everything.
try with
->whereHas('category' , function ($query) use ($filter) {
$query->where('name', 'like', "%$filter%");
})
instead of
->with(['category' => function ($query) use ($filter) {
$query->where('name', 'like', "%$filter%");
}])
with() will just loads the relationship not filtering result.

OrderBy its not wokring on laravel

i have a problem when doing "orderBy" name product. here are my codes
$resume = Transaction::with(['product' => function ($q) {
$q->orderBy('name_product','ASC');
}])
->where('status', 'keluar')
->where('status', 'masuk')
->get();
but my code its not working... here is the output
result
Use the collection to sort instead. It allows for sorting based on a nested property.
$resume = Transaction::with('product')
->where('status', 'keluar')
->where('status', 'masuk')
->get()
->sortBy('product.name_product')
->values();
Because you are applying condition to product, not in transaction.
I have another suggestion use whereHas.
Transaction::whereHas('product', function($query) {
$query->->orderBy('name_product','ASC');
})->where('status', 'keluar')
->where('status', 'masuk')
->get();

How to get first entry from the record in Laravel relation?

I got this query, it fetch results from my database, based on relationships.
The query works fine, but it grabs the first entry from the database/table and not the first value of the array.
How can I fix that?
$motor = Motor::query()
->where([
['effect_id', '=', $request->effect_id],
['poles', '=', $request->poles],
['voltage_id', '=', $request->voltage_id],
['active', '=', 1],
])
->whereHas('MotorSize', function (Builder $query) {
$query->whereIn('mounting', ['B5', 'B14', 'B34', 'B35']);
})
->firstOrFail();
I have tried different things, such as:
Adding the orderBy to the Motor model and created a "priority" field in my database, giving each a number between 1 and 100:
(i actually thought this would work, but it still grabs the first entry from the table)
public function MotorSize()
{
return $this->belongsTo('App\MotorSize')->orderBy('priority', 'ASC');
}
I also tried something like this, but it's a mess and never got it to work:
$query->where(function ($query) {
$query->where('mounting', 'like', 'B5');
})
->orWhere(function($query) {
$query->where('mounting', 'like', 'B14');
})
->orWhere(function($query) {
$query->where('mounting', 'like', 'B34');
})
->orWhere(function($query) {
$query->where('mounting', 'like', 'B35');
});
Any suggestions will be welcome :)

Laravel query builder with count of dependencies

I've got a problem with building query with Laravel (Lumen).
This is my code:
$user = User::where('name', $name)
->with(['pages' => function($query){
$query->orderBy('created_at', 'desc')
->with(['posts'])
->orderBy('created_at', 'desc')
->take(4);
}])
->first();
I would like to add to the response count of pages and posts so I want the response to have two more extra fields like:
...
pages_count: 5,
posts_count: 25
...
How can I do it?
Adding ->count() to queries doesn't work.
Thank you for your help.
You can use withCount like so:
User::where('name', $name)
->withCount('pages')
->with(['pages' => function($query){
$query->orderBy('created_at', 'desc')
->withCount('posts')
->with('posts')
->orderBy('created_at', 'desc')
->take(4);
}])
->first();

Categories