PHP code example of chandra-hemant / htkc-utils

1. Go to this page and download the library: Download chandra-hemant/htkc-utils library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chandra-hemant / htkc-utils example snippets


public static function getCustomModelData(
    Model $eloquentModel,
    array $dynamicConditions = [],
    bool|string $isFirst = false,
    int $limit = 0
)

$conditions = [
    [
        'method' => 'whereHas',
        'relation' => 'orders',
        'args' => ['status', '=', 'completed']
    ]
];
$result = CommonUtils::getCustomModelData($customerModel, $conditions);

public static function uploadFiles(
    Request $request,
    string $file,
    string $path,
    array $options = []
)

$options = [
    'prefix' => 'user_avatar',
    'disk' => 'public',
    'isArray' => false
];
$filePath = CommonUtils::uploadFiles($request, 'avatar', 'uploads/avatars', $options);

...

'uploads' => [
    'driver' => 'local',
    'root' => public_path('uploads'), // Files stored in public/uploads
    'url' => env('APP_URL') . '/uploads', // URL to access files
    'visibility' => 'public',
],

...

public static function alphaNumericGenerator(
    int $num,
    string $const = '',
    string $prefix_id = '',
    string $ref_id = '',
    string $prefix_char = ''
): string

$newId = CommonUtils::alphaNumericGenerator(
    4,
    'INV-',
    'Y22-',
    'INV-AA0001'
);

public static function dataSubmitRecursion(Model $model): bool

$saved = CommonUtils::dataSubmitRecursion($userModel);

public static function dataDeleteRecursion(Model $model): bool

$deleted = CommonUtils::dataDeleteRecursion($userModel);

public static function sendMail($recipient, $mailable, array $options = []): bool

$options = [
    'cc' => ['[email protected]'],
    'attachments' => [
        ['path' => 'invoices/latest.pdf', 'name' => 'Invoice.pdf']
    ]
];
$sent = CommonUtils::sendMail('[email protected]', new WelcomeMail(), $options);

public static function sendPushNotificationWithServerKey(
    string $title,
    string $body,
    $fcm_token,
    array $config,
    array $additionalData = []
)

    $title = "Hello";
    $body = "This is a test notification.";
    $fcm_token = "YOUR_FCM_DEVICE_TOKEN";

    $config = [
        'serverKey' => 'YOUR_SERVER_KEY_HERE',
        'url' => 'https://fcm.googleapis.com/fcm/send',
        'priority' => 'high',
        'android_channel_id' => 'high_importance_channel',
    ];

    $additionalData = [
        'custom_key' => 'custom_value',
    ];

    $response = FirebaseNotification::sendPushNotificationWithServerKey($title, $body, $fcm_token, $config, $additionalData);

    if ($response['status']) {
        echo $response['message'];
    } else {
        echo "Error: " . $response['message'];
        print_r($response['response']);
    }

public static function sendPushNotification(
    string $title,
    string $body,
    $fcm_token,
    string $serviceAccountPath,
    array $config = [],
    array $additionalData = []
): array

use ChandraHemant/HtkcUtils/FirebaseNotification;

// Define your service account key path
$serviceAccountPath = __DIR__ . '/path/to/service-account-file.json';

// Set the FCM token (single or array of tokens)
$fcmToken = 'YOUR_FCM_DEVICE_TOKEN';

// Define the notification details
$title = 'New Alert';
$body = 'You have a new message!';
$additionalData = [
    'user_id' => '123',
    'notification_type' => 'alert',
];

// Configuration for the request
$config = [
    'url' => 'https://fcm.googleapis.com/v1/projects/your_project_id/messages:send',
    'android_channel_id' => 'high_importance_channel',
];

// Send the push notification
$response = FirebaseNotification::sendPushNotification(
    $title,
    $body,
    $fcmToken,
    $serviceAccountPath,
    $config,
    $additionalData
);

// Output the result
if ($response['status']) {
    echo 'Notification sent successfully: ' . json_encode($response['response'], JSON_PRETTY_PRINT);
} else {
    echo 'Failed to send notification: ' . $response['message'];
    if (isset($response['response'])) {
        echo 'Error details: ' . json_encode($response['response'], JSON_PRETTY_PRINT);
    }
}

//Other example with more config

$config = [
    'url' => 'https://fcm.googleapis.com/v1/projects/YOUR_PROJECT_ID/messages:send',
    'android_channel_id' => 'high_importance_channel',
    'priority' => 'PRIORITY_HIGH',
    'with_sound' => true,
    'notification_sound' => 'custom_sound',
    'android_icon' => 'ic_notification',
    'color' => '#FF0000',
    'led_color' => '#00FF00',
    'led_on_ms' => '500ms',
    'led_off_ms' => '500ms',
    'image_url' => 'https://example.com/image.jpg',
    'category_id' => 'message_category',
    'actions' => [
        [
            'id' => 'reply',
            'title' => 'Reply',
            'icon_path' => 'ic_reply'
        ],
        [
            'id' => 'dismiss',
            'title' => 'Dismiss',
            'icon_path' => 'ic_dismiss'
        ]
    ]
];

// Send notification
$result = FirebaseNotification::sendPushNotification(
    'Title',
    'Body',
    'FCM_TOKEN',
    'path/to/service-account.json',
    $config,
    ['custom_data' => 'value']
);

use Illuminate\Http\Request;

$searchColumns = [
    // Specify your searchable columns here
];

$helper = new DynamicSearchHelper(
    request: $request,
    searchColumns: $searchColumns,
    withPagination: true,
    queryMode: false,
    isApi: true
);

$result = $helper->getDynamicSearchData();

// Define search value, columns, and relationships
$searchColumns = ['column1','relationshipMethod.column2','relationshipMethod1.relationshipMethod2.column3','relationshipMethod3.relationshipMethod4.relationshipMethod5.relationshipMethod6.column8'];

use Illuminate\Http\Request;
use ChandraHemant\HtkcUtils\DynamicSearchHelper;

class YourController extends Controller
{
    public function index(Request $request)
    {
        $user = Auth::user();

        $helper = new DynamicSearchHelper(
            request: $request,
            searchColumns: ['unique_id' => $user->unique_id],
            withPagination: true,
            queryMode: false,
            isApi: false,
        );
    
        // Get the dynamic search data
        $data = $helper->getDynamicSearchData();

        return view('your_view', compact('data'));
    }
}