0

So I have two functions named 'search' and 'pdfview'.

Every time I use the search function, I want to pass a query where it selects certain data to the pdfview function.

Here's my search function:

public function search(Request $request)
{
    if ($request->period == 'Daily') {
        $datefrom = Carbon::now()->startOfDay();
        $dateto = Carbon::now()->endOfDay();
    }
    elseif ($request->period == 'Weekly') {
        $datefrom = Carbon::now()->startOfWeek();
        $dateto = Carbon::now()->endOfWeek();
    }
    elseif ($request->period == 'Monthly') {
        $datefrom = Carbon::now()->startOfMonth();
        $dateto = Carbon::now()->endOfMonth();
    }
    elseif ($request->period == 'Yearly') {
        $datefrom = Carbon::now()->startOfYear();
        $dateto = Carbon::now()->endOfYear();   
    }
    else {
        $datefrom = Carbon::parse($request->datefrom);
        $dateto = Carbon::parse($request->dateto);
    }

    $solditems = DB::table('orders')
        // ->join('orders', 'receipts.receipt_id', '=', 'orders.receipt_id')
        ->whereDate('orders.created_at', '>=', $datefrom)
        ->whereDate('orders.created_at', '<=', $dateto)
        ->where('status','=', 'served')
        ->select('orders.*', DB::raw('SUM(subtotal) as subtotal'), DB::raw('SUM(qty) as qty'))
        ->groupBy('item_id')
        ->orderBy('created_at', 'dsc')
        ->get();

    return view('salesreports.sellingitems.index', compact('solditems'));
}

And here's my pdfview function:

public function pdfview(Request $request)
{
    $solditems = DB::table('orders')
        ->where('status', 'served')
        ->select('orders.*', DB::raw('SUM(subtotal) as subtotal'), DB::raw('SUM(qty) as qty'))
        ->groupBy('item_id')
        ->orderBy('qty', 'dsc')
        ->get();

    view()->share('solditems', $solditems);

    if ($request->has('download')) {
        $pdf = PDF::loadView('salesreports.sellingitems.pdf');
        return $pdf->download('sellingitems-' . Carbon::now() . '.pdf');
    }

    return view('salesreports.sellingitems.pdf');
}

As you can see, the pdfview function has a default query for the $solditems. But I wanted that variable to come from my search function.

UPDATE: I managed to solve the problem using session.

Here's how you store with session:

session()->put('period', 'Daily');

and this is how you retrieve with session:

$period = $request->session()->get('period', 'default');
13
  • do you mean, you want to make $solditems data in pdfview be same with search value? Commented Aug 1, 2017 at 6:19
  • @elegisandi Yes! Exactly like that. And it should update whenever the search function runs. Commented Aug 1, 2017 at 6:21
  • I think you have to define $datefrom and $dateto globally in controller, then these values will be changes in search function call and you can use them in pdfView function Commented Aug 1, 2017 at 6:23
  • you can store the $solditems data in search function to session, then just pull it on the pdfview function Commented Aug 1, 2017 at 6:23
  • 1
    @SagarGautam Ohh, haha. I tried initializing it inside the class with public static $datefrom = ' ' and tested it with return $datefrom; inside a function, it's says that it can't find that variable Commented Aug 1, 2017 at 6:48

2 Answers 2

0
public function getSolditems()
{
    if (!isset($this->solditems)) {
        $solditems = DB::table('orders')
            // ->join('orders', 'receipts.receipt_id', '=', 'orders.receipt_id')
            ->whereDate('orders.created_at','>=', $datefrom)
            ->whereDate('orders.created_at', '<=', $dateto)
            ->where('status','=', 'served')
            ->select('orders.*', DB::raw('SUM(subtotal) as subtotal'), DB::raw('SUM(qty) as qty'))
            ->groupBy('item_id')
            ->orderBy('created_at','dsc')
            ->get();
        $this->solditems = $solditems;
    } else {
        $solditems = $this->solditems;
    }

    return $solditems;
}

public function search(Request $request)
{
    ....
    $solditems = getSolditems();
    ....
}

public function pdfview(Request $request)
{
    ....
    $solditems = getSolditems();
    ....
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think you can use $request->merge() method to pass $solditems data to pdfview() method from search()

public function search(Request $request)
{
   ....
   $request->merge(['solditems'=>$solditems]);
   $this->pdfview()
}


public function pdfview(Request $request)
{
   $solditems = $request->get('solditems');
   ....
}

2 Comments

It gives an error that it should have a Request Instance, and when I do add it. $solditems can't be found at pdfview function.
In search update $this->pdfview() to $this->pdfview($request) and then try

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.