Aronaman
3 months ago
436
7
Laravel

conver to database query builder

Posted 3 months ago by Aronaman

any help, change a given eloquent query to database query

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

// change $organizations to database query 
$organizations = Organization::withCount(['rooms','confrences'])->with(['typeOfOrganization','user'])
->where('approved', 1)->when(request('type_of_organization'), function($query){ return $query->where('organizations.type_of_organization_id', 	request('type_of_organization')); })

->when($request->has(['start_price', 'end_price']), function ($query) use ($request, $exchangeRate) {
    return $query->where(function ($query) use ($request) {
      $query->whereBetween('avg_price', [
      $request->start_price, $request->end_price
     ])->where('currency_id', 1);
      })->orWhere(function ($query) use ($request, $exchangeRate) {
        $query->whereBetween('avg_price', [
        $request->start_price * $exchangeRate, $request->end_price * $exchangeRate
      ])->where('currency_id', 2);
   });
 })->get();

thanks in advance

Please sign in or create an account to participate in this conversation.