I wanna get data which is pwra_dt = $month && $year. What can I do? For now, it shows all records!!
Query:
$purchaseOrder = PurchaseOrder::whereHas('pwra', function (Builder $query) use ($year, $month) {
$query->whereYear('pwra_dt', $year)
->whereMonth('pwra_dt', $month);
})
->get();
PurchaseOrder(Model)
public function pwra()
{
return $this->hasMany('App\Models\Pwra', 'purchase_order_uuid', 'purchase_order_uuid');
}
PWRA (Model)
public function purchaseOrder()
{
return $this->belongsTo('App\Models\PurchaseOrder', 'purchase_order_uuid', 'purchase_order_uuid');
}
Related
I have 3 model Transaction , Cart , Acceptedcart.
in Cart model I have transaction_id and in Acceptedcart model have cart_id.
in Transaction.php my relationship to carts:
public function carts()
{
return $this->hasMany(Cart::class);
}
in Cart.php my relationship to accepted carts :
public function accepted_carts()
{
return $this->hasMany(Acceptedcart::class);
}
And the query to get the transactions where acceptedcart->sended== false:
$transactions = Transaction::whereHas('carts.accepted_carts', function ($query) {
$query->where('sended', false);
})->get();
Now I want to get sum() of count column in acceptedcart, where I can set the sum()?
$transactions = Transaction::whereHas('carts.accepted_carts', function ($query) {
$query->where('sended', false);
})->sum('count);
Or
$transactions = Transaction::whereHas('carts.accepted_carts', function ($query) {
$query->where('sended', false)->sum('count');
})->get();
is there Only one way that foreach on the ->get() result ?
Try this
Transaction.php
public function accepted_carts()
{
return $this->hasManyThrough(Acceptedcart::class, Cart::class);
}
Query
$transactions = Transaction::query()
->whereHas('accepted_carts', fn ($query) => $query->where('sended', false))
->withCount([
'accepted_carts' => fn ($query) => $query->select(DB::raw('SUM(count) as accepted_carts_sum'))
])
->get();
public function scopeSearch($query, $value)
{
$searchValues = explode(' ', $value);
if (!$value) return $query;
return $query->where(function ($q) use ($searchValues) {
foreach ($searchValues as $token) {
$q->orWhere('name', 'like', "%{$token}%");
$q->orWhere('street', 'like', "%{$token}%");
}
});
}
I want to search the data. This model also has
public function brands()
{
return $this->belongsToMany(Brand::class, 'dealer_brands');
}
public function province()
{
return $this->belongsTo(Province::class);
}
How can I get data from the relastionship. Like Dealer(model) has data Nmae = josh , brand_id = 1 {brand.name = samsung} , province_id = 2 (province.name = "aligora"). When I search Josh Samsung Alogora, I want to ge the data. When I only search aligora, I want to get the data of model having province aligora. hOW CAN I MODIFY CODE?
looking at your model relationships. This may work for you
public function scopeSearch($query, $value)
{
if (!$value) return $query;
$searchValues = explode(' ', $value);
return $query->where(function ($q) use ($searchValues) {
foreach ($searchValues as $token) {
$q->where('name', 'like', "%{$token}%")
->orWhere('street', 'like', "%{$token}%")
->orWhereHas('brands', function ($sub_q) use ($searchValues) {
$sub_q->where('name', 'like', "%{$token}%")
->orWhere('street', 'like', "%{$token}%");
})
->orWhereHas('province', function ($sub_q) use ($searchValues) {
$sub_q->where('name', 'like', "%{$token}%")
->orWhere('street', 'like', "%{$token}%");
});
}
});
}
I have no idea about column names in related tables so repeating name and street. you can change as per requirement
Is that any method to filter the employees and the manpower table record does not return also?
$result = Pwra::with('purchaseOrder', 'manpower')
->where('pwra_dt', $date)
->where('time_session', $session)
->whereHas('manpower.employees', function ($q) {
$q->where('status', 1);
})
->get();
Pwra Class
public function manpower()
{
return $this->hasMany('App\Models\Manpower', 'pwra_uuid', 'pwra_uuid');
}
Manpower Class
public function employee()
{
return $this->hasOne('App\Models\Employees', 'employees_uuid', 'employees_uuid')->where('status', 1);
}
What I expected is: When the employees Status = 0, it will not return any record even manpower.
you can try 2 level whereHas()
$result = Pwra::with('purchaseOrder', 'manpower')
->where('pwra_dt', $date)
->where('time_session', $session)
->whereHas('manpower', function ($q) {
$q->whereHas('employees',function($subQ){
$subQ->where('status', 1);
});
})
->get();
whereHas('manpower.employees' it will not work
I'am new with laravel. How can I combine both of these conditions?
public function student($classroom_id)
{
$members = Classroom::findOrFail($classroom_id,)->members->load('role');
$members = User::where([
['role_id', 2]
])->get();
return response()->json($members);
}
Try this:
public function student($classroom_id)
{
$members = Classroom::whereHas('members.role', function ($query) {
$query->where('id', '2');
})->findOrFail($classroom_id)->members->load('role');
return response()->json($members);
}
The first 2 functions are for the dropdown, and the last function filter function is to fetch the data into datatable. I want to use the results from function FirstName ($FirstName= [];) and LastName ($LastName= [];) for this purpose.
public function FirstName(Request $request)
{
$FirstName= [];
if($request->has('q')){
$search = $request->q;
$FirstName= DB::table("user")
->select("id","First_Name")
->where('First_Name','LIKE',"%$search%")
->get();
}
return response()->json($FirstName);
}
public function LastName(Request $request)
{
$LastName= [];
if($request->has('q')){
$search = $request->q;
$LastName= DB::table("user")
->select("id","Last_Name")
->where('Last_Name','LIKE',"%$search%")
->get();
}
return response()->json($Last_Name);
}
public function filter()
{
if(!empty($FirstName)) //catch from other function
{
$data = DB::table('user')
->select('id', 'Fn', 'Ln')
->where('First_Name', $FirstName)
->where('Last_Name', $LastName)
->get();
}
else
{
$data = DB::table('user')
->select('id', 'Fn', 'Ln')
->get();
}
return datatables()->of($data)->make(true);
$data = DB::table('user')
->select('Last_Name','First_Name')
->groupBy('First_Name')
->groupBy('Last_Name')
->orderBy('First_Name', 'ASC')
->orderBy('Last_Name', 'ASC')
->get();
return response()->json($data);
}
What I did above is put the array from function first and last name to the filter function. But i got it wrong. What should be done to fix this?