Description
I have integrated wallet in my system , my system structure has many models which using the wallet , like Company , Subscriber, Invoice, Revenue , I have a page where I listing all the transactions without any issue , my problem is with globalfilter in datatable , I want the admin to filter the name or username of the company or customer who made this transaction , or the transactiosn that received from the searched word
actually I have override the Transaction Model and I added these 2 relations
public function deposit_transfer(){
return $this->hasOne(\Bavix\Wallet\Models\Transfer::class,"deposit_id","id")->withoutGlobalScope("company_id");
}
public function withdraw_transfer(){
return $this->hasOne(TransferOver::class,"withdraw_id","id")->withoutGlobalScope("company_id");
}
now when I am retrieving the data using this query
$queryBuilder= Transaction::query()->with(["deposit_transfer","deposit_transfer.from","deposit_transfer.from.holder","withdraw_transfer","withdraw_transfer.to","withdraw_transfer.to.wallet.holder"])-> withoutGlobalScope('company_id');
and this is how I am showing the name or username(in case of subscriber) of the owner of the transaction using yajra datatable
->addColumn('username', function ($data) {
$type=$data->type;
if($type=="deposit")
{
$relation=$data->deposit_transfer;
if($relation->from->holder_type==Revenue::class){
if(isset($relation->from->holder->invoice->customer->username ))
return "<a href='/ISP/userview/".$relation->from->holder->invoice->customer->id."'>".$relation->from->holder->invoice->customer->username."</a>" ;
}
elseif($relation->from->holder_type==Invoice::class){
if(isset($relation->from->holder->customer->username ))
return "<a href='/ISP/userview/".$relation->from->holder->customer->id."'>".$relation->from->holder->customer->username."</a>" ;
}
else if($relation->to_type==Company::class){
if(isset($relation->from->holder->name ))
return $relation->from->holder->name??"N\a" ;
return $data->company->name;
}
else if($relation->from->holder_type==Totalrevenue::class){
return $data->company->name;
}
}
elseif($type=="withdraw"){
$relation=$data->withdraw_transfer;
if(isset($relation)&&$relation->from_type ==Wallet::class &&$relation->to_type==Invoice::class){
if(isset($relation->to->customer->username ))
return "<a href='/ISP/userview/".$relation->to->customer->username."'>".$relation->to->customer->username."</a>" ;
}
elseif(isset($relation)&&$relation->from_type ==Wallet::class &&$relation->to_type==Revenue::class){
if(isset($relation->to->invoice->customer->username ))
return "<a href='/ISP/userview/".$relation->to->invoice->customer->username."'>".$relation->to->invoice->customer->username."</a>" ;
}
else{
if($data->payable_type==Company::class)
{
if(isset($relation->to->name))
return $relation->to->name??"N\A";
return $data->company->name??"N\A";
}
return "N\A";
}
}
if($data->to_type =='App\\Models\\Invoice')
{ if(isset($data->to->customer->username ))
return "<a href='/ISP/userview/".$data->to->customer->id."'>".$data->to->customer->username."</a>" ;
else
return "N\a";
}
})
the records are coming right without any issue , but I am not able to filter actually
this is my filter code
->filterColumn('username', function($query, $keyword) {
$query->where(function($q) use ($keyword){
return $q->whereHas("withdraw_transfer", function ($q) use ($keyword) {
return $q->where('to_type', Invoice::class)->wherehas('to', function ($q) use ($keyword) {
return $q-> wherehas('wallet', function ($q) use ($keyword) {
return $q->where('holder_type', Invoice::class)->wherehas("holder",function($q)use ($keyword){
return $q-> wherehas('customer', function ($q) use ($keyword) {
});
});
});
});
});
});
this is only in case of withdraw , as I am trying with it right now , but it is not working as expected and it return error that saying Revenue not has customer relation , I am not sure why it is trying with revenue model although I am seting the holder type to be invoice .
can u help me please