samedi 30 mars 2019

Create condition for dateRange with another associated fields in laravel

I am trying to create a sales filtering page where the user can set the dateRange and if they want they can also set the currency and the customer's name as part of the filter. here's the screenshot.

enter image description here

And here's my code;

public function filterSale(Request $request)
{

    $customer = \DB::table('companies')->where('category', '=', 'customer')->pluck('comp_name','id')->all();
    $currencies = \DB::table('currencies')->orderBy('default', 'desc')->pluck('acronym','id')->all();

    $currency = $request['currency_id'];
    $company = $request['company_id'];

    if($request->from_date != '' && $request->to_date != ''||$request['currency_id']!=''||$request['company_id']!='')
    {
        $sales = Sales::where('publish','=',1)
                ->whereBetween('created_at', array($request->from_date, $request->to_date))
                ->where('currency_id','=', $currency)
                ->where('company_id','=', $company)
                ->get();

        return view ('reports.salesReport', compact ('sales', $sales))
                                            ->with ('currency', $currency)
                                            ->with ('company', $company)
                                            ->with('customer', $customer)
                                            ->with('currencies', $currencies);
    }

What I am trying to achieve is that if the user set the dateRage and leave the currency and customer empty, the the data should display all regardless of their currency and which customer it belong to.

OR Customer + datetrage OR currency + dateRange OR currency + customer + daterage.

how can I achieve that?

Thank you so much in advance!

Aucun commentaire:

Enregistrer un commentaire