Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Aronaman's avatar

how can i shorten this code

it is working, but the only change is the start, end price value, how can I shorten this code without using merge

 $exchangeRateToDollar=Currency::where('currencyType','ETB')->firstOrFail()->exchangeRate;
 $startPriceDollar =$request->start_price*$exchangeRateToDollar;
 $endPriceDollar =$request->end_price*$exchangeRateToDollar;

 $organizationBirr= Organization::withCount(['rooms','confrences'])
        ->with(['typeOfOrganization','user'])->where('approved',1)
        ->when(request(['start_price','end_price']), function($query){
              return $query->where('organizations.avg_price','>=', request('start_price'))
                           ->where('organizations.avg_price','<=',request('end_price'));
            })})->get();


$organizationDollar=Organization::withCount(['rooms','confrences'])
            ->with(['typeOfOrganization','user'])->where('approved',1)
	    ->when(request(['start_price','end_price']), function($query) use ($startPriceDollar,$endPriceDollar){
                  return $query->where('organizations.avg_price','>=', $startPriceDollar)
                               ->where('organizations.avg_price','<=',$endPriceDollar);
                })->get();


 $organizations=$organizationBirr->merge($organizationDollar)->paginate(21);
0 likes
3 replies
walidabou's avatar
Level 2

Try this

$exchangeRateToDollar = Currency::where('currencyType','ETB')->firstOrFail()->exchangeRate;
$startPriceDollar = $request->start_price * $exchangeRateToDollar;
$endPriceDollar = $request->end_price * $exchangeRateToDollar;

$organizations = Organization::withCount(['rooms','confrences'])->with(['typeOfOrganization','user'])->where('approved', 1)->when(request(['start_price', 'end_price']), function($query) use ($startPriceDollar, $endPriceDollar) {
			return $query->where(function($q) {
			 return $q->where('organizations.avg_price', '>=', request('start_price'))->where('organizations.avg_price', '<=', request('end_price'));
			})->orWhere(function($q) {
			 return $q->where('organizations.avg_price', '>=', $startPriceDollar)->where('organizations.avg_price', '<=', $endPriceDollar);
	});
})->get();

$organizations = $organizations->paginate(21);
1 like
Aronaman's avatar

@walidabou thanks very much. it work add use() on second function also. thanks for your help

MichalOravec's avatar

@aronaman Change it to this

$exchangeRate = Currency::where('currencyType', 'ETB')->firstOrFail()->exchangeRate;

$organizations = Organization::with(['typeOfOrganization', 'user'])->withCount(['rooms', 'confrences'])->when($request->has(['start_price', 'end_price']), function($query) use ($request, $exchangeRate) {
    return $query->whereBetween('avg_price', [
        $request->start_price, $request->end_price
    ])->orWhereBetween('avg_price', [
        $request->start_price * $exchangeRate, $request->end_price * $exchangeRate
    ]);
})->where('approved', 1)->paginate(21);

Please or to participate in this conversation.