App\services\BaseService::bulk PHP Method

bulk() public method

public bulk ( $ids, $action ) : integer
$ids
$action
return integer
    public function bulk($ids, $action)
    {
        if (!$ids) {
            return 0;
        }
        $entities = $this->getRepo()->findByPublicIdsWithTrashed($ids);
        foreach ($entities as $entity) {
            if (Auth::user()->can('edit', $entity)) {
                $this->getRepo()->{$action}($entity);
            }
        }
        return count($entities);
    }

Usage Example

 public function bulk($ids, $action, $params = [])
 {
     if ($action == 'refund') {
         if (!$ids) {
             return 0;
         }
         $payments = $this->getRepo()->findByPublicIdsWithTrashed($ids);
         $successful = 0;
         foreach ($payments as $payment) {
             if (Auth::user()->can('edit', $payment)) {
                 $amount = !empty($params['amount']) ? floatval($params['amount']) : null;
                 $accountGateway = $payment->account_gateway;
                 $paymentDriver = $accountGateway->paymentDriver();
                 if ($paymentDriver->refundPayment($payment, $amount)) {
                     $successful++;
                 }
             }
         }
         return $successful;
     } else {
         return parent::bulk($ids, $action);
     }
 }
BaseService