0

In view, there's 2 tables and implemented the pagination for both but having a problem in the buttons

When we tried to change the page of table 1, the table 2 is also changed

In the controller, there's 4 model: 1) Purchase; 2) Direct Payments; 3) Sales; 4) Direct Collections.

purchases columns

id
number,
date_delivered

direct payments columns

id,
number
date

After getting the data from the perspective model we, rearrange the format

**Rearrange the format **

public function setToInputOutputTax($data,$module,$module_id)
{
    $items                                  = [];
    foreach($data as $d => $datum) {
         $items[] = [
            'id'                 => $datum->id,
            'checkbox'           => 0,
            // 'vat_type'           => $vat_type,
            'transaction_date'   => isset($datum->date_delivered) ? $datum->date_delivered : $datum->date,
            'transaction_number' => $module . $datum->number,
            'module_id'          => $module_id,
            'vat_id'             => $datum->vat->id,
            'vat_code'           => $datum->vat->code.' - '.$datum->vat->name,
            'vat_rate'           => $datum->vat_rate,
            'net_of_vat'         => $datum->net_of_vat,
            'amount_due'         => $datum->amount_due,
            'balance'            => $datum->vat_balance,
            'amount_to_apply'    => $datum->vat_balance,
        ];
    }

    return $items;
}

We merged the purchase & direct payments, same goes with sale & direct collection

    $purchases                              = Purchase::select(
                                                'id',
                                                'number',
                                                'date_delivered',
                                                'vat_id',
                                                'vat_rate',
                                                'net_of_vat',
                                                'amount_due',
                                                'vat_balance'
                                            )
                                            ->where('level_id',4)
                                            ->where('vat_balance','>', 0)
                                            ->where('company_id',Auth::user()->company_id)
                                            ->search(trim($this->input_search))
                                            ->orderBy('id', $this->order)
                                            ->get();
                                       
$purchases_input_tax                    = $this->setToInputOutputTax($purchases,'P-',201);                                        


$direct_payments                        = DirectPayment::select(
                                                'id',
                                                'number',
                                                'date',
                                                'vat_id',
                                                'vat_rate',
                                                'net_of_vat',
                                                'amount_due',
                                                'vat_balance'
                                            )
                                            // ->union($purchases)
                                            ->where('level_id',4)
                                            ->where('vat_balance','>', 0)
                                            ->where('company_id',Auth::user()->company_id)
                                            ->search(trim($this->input_search))
                                            ->orderBy('id', $this->order)
                                            ->get();
                                            // ->paginate(10, ['*'], 'input');

$direct_payments_input_tax              = $this->setToInputOutputTax($direct_payments,'DP-',210);
$input_tax                              = array_merge($purchases_input_tax,$direct_payments_input_tax);

$col                                    = collect($input_tax);


$input_currentPage                      = Paginator::resolveCurrentPage();


$currentPageItems                       = $col->slice(($input_currentPage - 1) * $this->input_size, $this->input_size)->all();

$all_input_taxes                        = new Paginator($currentPageItems, count($col), $this->input_size);

$sales                                  = Sale::where('level_id',4)
                                            ->where('vat_balance','>', 0)
                                            // ->where('status_id',[3,9])
                                            ->where('company_id',Auth::user()->company_id)
                                            ->search(trim($this->output_search))
                                            ->orderBy('id', $this->order)
                                            ->get();
                                            // ->paginate(10, ['*'], 'output');

$sales_output_tax                       = $this->setToInputOutputTax($sales,'S-',101);                                        

$direct_collections                     = DirectCollection::where('level_id',4)
                                            ->where('vat_balance','>', 0)
                                            ->where('company_id',Auth::user()->company_id)
                                            ->search(trim($this->output_search))
                                            ->orderBy('id', $this->order)
                                            ->get();
                                            // ->paginate(10, ['*'], 'output');

$direct_collections_output_tax          = $this->setToInputOutputTax($sales,'DC-',110);                                            

$output_tax                             = array_merge($sales_output_tax,$direct_collections_output_tax);

$output_col                             = collect($output_tax);

$output_currentPage                     = Paginator::resolveCurrentPage();

$output_currentPageItems                = $output_col->slice(($output_currentPage - 1) * $this->output_size, $this->output_size)->all();

$all_output_taxes                        = new Paginator($output_currentPageItems, count($output_col), $this->output_size);

Question: Is it possible to convert the array to same format of the model? Reason why we need to do this is because we need to paginate the new format

Angel
  • 966
  • 4
  • 25
  • 60
  • I can see that you did some concatenation and default value setting in your setToInputOutputTax() method which is actually feasible inside the query builder. In case you want to stick with your implementation, look up for Laravel Collections. This is the instance returned by Query Buillder. You can also check related question here https://stackoverflow.com/questions/49083535/convert-array-to-collection-in-laravel – Erlisar Vasquez Nov 09 '22 at 02:01

0 Answers0