(errno: 150 “Foreign key constraint is incorrectly formed “)”)

I am trying to create scheduleUser migration but this error is popping out although I am using the same standards I’ve been taught. I created another schema it gave me the same error. I made sure the relationships is written in a correct way still having the same error. I am using Laravel v.12.

This is the scheduleUser migration:

<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('scheduelusers', function (Blueprint $table) {
            
            $table->id();
            // $table->unsignedBigInteger('studentId')-> primary()->nullable() -> from(100000);
            $table -> string('firstName');
            $table -> string ('middleName');
            $table -> string ('lastName');
            $table -> string ('email')-> unique();
            $table -> string ('password');
            $table -> string ('phone')-> unique();
            $table -> foreignId('role_id')->constrained();
            
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('scheduelusers');
    }
};

This is the function I am using in scheduleusers migration for the relationship:

    public function role()
    {
        return $this->belongsTo(Role::class);
    }
    
}

This is the role migration:

<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->id();
            $table -> string('role');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('roles');
    }
};

This is the function in role model for the relationship:

    public function scheduleusers()
    {
        return $this->hasMany(ScheduleUser::class);
    }
}

Laravel N+1 when accessing a pivot relationship and the related model (unit)

I got n+1 query while working with my models and pivot

Recipe.php:

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function ingredients(): BelongsToMany
    {
        return $this->belongsToMany(Ingredient::class)
            ->using(IngredientRecipe::class)
            ->withTimestamps()
            ->withPivot(['quantity', 'unit_id']);
    }

IngredientRecipe.php (Pivot):

    public function unit(): BelongsTo
    {
        return $this->belongsTo(Unit::class);
    }

Ingredient.php:

    public function recipes(): BelongsToMany
    {
        return $this->belongsToMany(Recipe::class);
    }

Unit.php:

    public function ingredient_recipes(): HasMany
    {
        return $this->hasMany(IngredientRecipe::class);
    }

What am I trying to do:

In my user-profile page I want to show a list of recipes for owner (user). Every recipe has ingredients() which are in pivot table with additionall columns quantity and unit_id

Code:

In my ProfileController.php I am using this code:

    public function show_profile(User $user)
    {
        $userRecipes = $user->recipes()->with('ingredients', 'guideSteps')->get();

        return view('user.user-profile', compact('user', 'userRecipes'));
    }

Problem:

Laravel doesn’t know that pivot->unit_id is related to the Unit model, so every I am accessing pivot->unit, it makes a separate query to the units table.

In debugbar I am getting 15 duplicated queries:

select * from users where users.id = 1 limit 1

select * from units where units.id = 3 limit 1

select * from units where units.id = 3 limit 1

… 12 more

And a problem is in this place:


   @foreach($userRecipes as $recipe)
       <x-recipe-card :recipe="$recipe"/>
   @endforeach

---------------inside component recipe-card:------------------------

        @foreach($recipe->ingredients as $ingredient)
            <div>
                <span>{{ $ingredient->name }}</span>
                <div></div>
                <span>{{ $ingredient->pivot->quantity . ' '. $ingredient->pivot->unit->name}}</span>
            </div>
        @endforeach

Troubleshooting Laravel Website Issues After Hosting on Shared Server

I have developed a Laravel website and uploaded it to the live server. Since a Laravel website cannot be hosted directly on shared hosting, I created an ASSET_URL in the .env file and moved the index.php file, which was inside the public folder, to the main directory of the website. I also changed the default session time of 120 in the .env file to SESSION_LIFETIME=600 and also have the setting SESSION_SECURE_COOKIE =true. Now, the website is working fine. However, when I keep the website open in a browser for a couple of days, I face issues like some buttons not working, some links not working, and the page reloading when I click a button. However, when I change the browser or open it in incognito mode, the website works perfectly. I don’t want to clear the cache all the time because the end user may not be aware of browser cache, etc. How to resolve this

How to write the correct regular expression in php?

I need to cut all tags like
<span style="background-color:SOME_COLOR">test</span>
and ONLY if style is background-color, not any others. Also, all text inside the tag must be saved. I tried to use something like this, but it doesn`t work

$pattern = '#<span style="background-color:s*[^;]+;">(.*?)</span>#ui';
$result = preg_replace($pattern, '$1', $inputString);```

USDC transfer on Polygon using simple-web3-php succeeds without error but tokens not moved

I’m using drlecks/simple-web3-php v0.10.0 to transfer USDC on Polygon (contract: 0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359). I have the sender’s private key and successfully use the library to send USDT with the same approach.

But when sending USDC, the transaction shows up on the blockchain, but:

  • No USDC is actually transferred.
  • The wallet balance doesn’t change.
  • I see two ERC-20 transfer events, one of them has amount 0.

There’s no error returned from the transfer function, but the transfer clearly doesn’t complete properly.

I’m using the same transfer(address,uint256) method, and everything works for USDT. Why would USDC behave differently? How can I fix this using only drlecks/simple-web3-php?

Code snippets :

class Web3
{
    private $web3;

    public function __construct(private $contractAddress = '', private $contractABI = '', private $chainId = 0, private $transactionId = 0)
    {
        $productionBaseUrl = 'https://polygon-rpc.com/';
        $stagingBaseUrl = 'https://rpc-amoy.polygon.technology/';
        $uri = Core::$debug ? $stagingBaseUrl : $productionBaseUrl;
        $this->web3 = new SWeb3($uri);
        $this->web3->chainId = $this->chainId;
        $this->contractABI = html_entity_decode($this->contractABI);
    }

    public function transfer($address, $privateKey, $amount)
    {
        $config = $this->config($address, $privateKey);
        if ($config->code != 1) {
            return $config;
        }

        $responseConvertValue = $this->convertValue(strval($amount), 'transferUsdt');

        if ($responseConvertValue->code != 1) {
            return $responseConvertValue;
        }

        $extraData = [];
        if (json_decode($this->web3->personal->getNonce()->value)) {
            $extraData = ['nonce' => $this->web3->personal->getNonce()];
        } else {
            $extraData = ['nonce' => ''];
        }
        $gasEstimateResult = $this->web3->call('eth_estimateGas', [$extraData]);
        if (!isset($gasEstimateResult->result)) {
            $this->handleError('transferUsdt', 'Cant get estimateGas');
            return (object) ['code' => -3, 'message' => 'Cant get estimateGas'];
        }
        $extraData['gasPrice'] = $this->web3->getGasPrice();
        $extraData['gasLimit'] = $this->web3->utils->hexToBn($gasEstimateResult->result);
        try {
            $contract = new SWeb3_contract($this->web3, $this->contractAddress, $this->contractABI);
            $result =
                $contract->send('transfer', [env('MAIN_ADDRESS_WALLET'), $responseConvertValue->value], $extraData);
            if (isset($result->error)) {
                $message = 'Code :' . $result->error->code . 'Error :' . $result->error->message;
                $this->handleError('transferUsdt', $message);
                return (object) ['code' => -1, 'message' => $result->error->message];
            }
        } catch (Throwable $th) {
            $this->handleError('transferUsdt', $th->getMessage());
            return (object) ['code' => -1, 'message' => $th->getMessage()];
        }
        return (object) ['code' => 1, 'message' => $result];
    }
    private function convertValue($value, $methodName)
    {

        try {
            $value = Utils::toWei($value, $methodName == 'transferUsdt' ? "mwei" : 'ether');
        } catch (Throwable $th) {
            $this->handleError($methodName . ' > convertValue >', $th->getMessage());
            return (object) ['code' => -1, 'message' => $th->getMessage()];
        }
        return (object) ['code' => 1, 'value' => $value];
    }

A snippet of a single transmission attempt (https://i.sstatic.net/KPCILlFG.png)

Erreur malgré upload du bon fichier [closed]

Je suis confronté à un sérieux problème. J’ai un plugin wordpress dans lequel je souhaite récupérer la preuve de paiement avant de valider la commande. Malgré mes nombreux essaie je ne trouve pas ayant déjà essayer avec ce code, ça me renvoie l’erreur veuillez televerser un fichier même si je l’ai déjà televerser . Voici le fichier :

<?php

// Vérifie si WooCommerce est activé
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    add_action( 'plugins_loaded', 'momo_paid_payment_gateway' );
}

function momo_paid_payment_gateway() {
    class momo_paid_payment_gateway extends WC_Payment_Gateway {

        public function __construct() {
            $this->id                 = 'momo-paid';
            $this->icon               = plugins_url( '../images/icons.png', __FILE__ );
            $this->method_title       = __( 'MomoPaid', 'momo-paid' );
            $this->title              = $this->method_title;
            $this->has_fields         = true;
            $this->init_form_fields();
            $this->init_settings();
            $this->enabled            = $this->get_option( 'enabled' );
            $this->description        = __( 'MomoPaid: Recevez vos Paiements en ligne par MTN Mobile Money', 'momo-paid' );
            $this->method_description = $this->description;

            $this->recipient_number   = $this->get_option( 'recipient_number' );
            $this->recipient_name     = $this->get_option( 'recipient_name' );

            add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
            add_action( 'wp_enqueue_scripts', array( $this, 'payments_scripts' ) );
            add_action( 'woocommerce_checkout_process', array( $this, 'validate_fields' ) );
            add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'save_post_meta' ) );
            add_action( 'woocommerce_admin_order_data_after_order_details', array( $this, 'display_transaction_id' ), 10 );
            add_action( 'woocommerce_admin_order_data_after_order_details', array( $this, 'display_proof_file' ), 20 );
        }

        public function init_form_fields() {
            $this->form_fields = array(
                'enabled' => array(
                    'title'   => __( 'Activer/Desactiver MomoPaid', 'momo-paid' ),
                    'type'    => 'checkbox',
                    'label'   => __( 'Activer/Desactiver MomoPaid', 'momo-paid' ),
                    'default' => 'no',
                ),
                'recipient_name' => array(
                    'title'       => __( 'Identité du Bénéficiaire', 'momo-paid' ),
                    'type'        => 'text',
                    'description' => __( 'Nom du Bénéficiaire du paiement MTN Momo. <u><strong>E.g :</strong></u> Mamadou Diabate', 'momo-paid' ),
                ),
                'recipient_number' => array(
                    'title'       => __( 'Numéro du Bénéficiaire MTN Mobile Money', 'momo-paid' ),
                    'type'        => 'number',
                    'description' => __( 'Numéro du Bénéficiaire du Paiement. <u><strong>E.g :</strong></u> 2290197965267', 'momo-paid' ),
                ),
            );
        }

        public function payment_fields() {
            if ( is_checkout() ) {
                $recipient_name   = $this->recipient_name;
                $recipient_number = $this->recipient_number;

                if ( ! empty( $recipient_name ) || ! empty( $recipient_number ) ) {
                    momo_paid_payment_using_mobile_solutions( $recipient_name, $recipient_number );

                    echo '<br/></BLOCKQUOTE><div class="momo-money-form">';
                    woocommerce_form_field( '_momo_money', array(
                        'type'        => 'number',
                        'label'       => __( 'VÉRIFICATION MTN MOMO', 'momo-paid' ),
                        'placeholder' => __( '(XXXXXXXXXX)', 'momo-paid' ),
                        'required'    => true,
                        'class'       => array( 'form-row-wide' ),
                    ) );
                    echo '</div>';

                    echo '<div class="form-row form-row-wide">
                            <label for="momo_proof">' . esc_html__( 'Preuve de paiement (JPG, PNG, PDF – max 5MB)', 'momo-paid' ) . '</label>
                            <input type="file" name="momo_proof" id="momo_proof" accept=".jpg,.jpeg,.png,.pdf" required>
                          </div>';
                    echo '<br/><div class="momo-paid-know-before"></div>';
                } else {
                    esc_html_e( "Veuillez-bien contacter l'administrateur de ce site, un problème est survenu.", 'momo-paid' );
                }
            }
        }

        public function validate_fields() {
            if ( isset( $_POST['_momo_money'] ) ) {
                $transaction_id = sanitize_text_field( $_POST['_momo_money'] );
                if ( empty( $transaction_id ) ) {
                    wc_add_notice( __( 'L'ID de Transaction Momo Money ne peut être vide.', 'momo-paid' ), 'error' );
                } elseif ( ! preg_match( '/^d{10}$/', $transaction_id ) ) {
                    wc_add_notice( __( 'La valeur de la transaction doit contenir exactement 10 chiffres.', 'momo-paid' ), 'error' );
                }
            }

            if ( empty( $_FILES['momo_proof']['name'] ) ) {
                wc_add_notice( __( 'Veuillez fournir une preuve de paiement.', 'momo-paid' ), 'error' );
            } else {
                $file     = $_FILES['momo_proof'];
                $allowed  = array( 'jpg', 'jpeg', 'png', 'pdf' );
                $ext      = strtolower( pathinfo( $file['name'], PATHINFO_EXTENSION ) );
                $max_size = 5 * 1024 * 1024;

                if ( ! in_array( $ext, $allowed ) ) {
                    wc_add_notice( __( 'Format de fichier invalide. Seuls JPG, PNG et PDF sont autorisés.', 'momo-paid' ), 'error' );
                } elseif ( $file['size'] > $max_size ) {
                    wc_add_notice( __( 'Fichier trop volumineux. 5MB max.', 'momo-paid' ), 'error' );
                }
            }
        }

        public function save_post_meta( $order_id ) {
            if ( isset( $_POST['_momo_money'] ) ) {
                $transaction_id = sanitize_text_field( wp_unslash( $_POST['_momo_money'] ) );
                if ( preg_match( '/^d{10}$/', $transaction_id ) ) {
                    update_post_meta( $order_id, '_momo_money', $transaction_id );
                }
            }

            if ( ! empty( $_FILES['momo_proof']['name'] ) ) {
                require_once ABSPATH . 'wp-admin/includes/file.php';
                $uploaded = wp_handle_upload( $_FILES['momo_proof'], array( 'test_form' => false ) );
                if ( ! isset( $uploaded['error'] ) ) {
                    update_post_meta( $order_id, '_momo_proof_url', esc_url( $uploaded['url'] ) );
                }
            }
        }

        public function display_transaction_id( $order ) {
            if ( 'momo-paid' === $order->get_payment_method() ) {
                $transaction_id = get_post_meta( $order->get_id(), '_momo_money', true );
                if ( $transaction_id ) {
                    echo '<div class="order_data_column"><h4>' . esc_html__( 'NUMÉRO DE RÉFÉRENCE :', 'momo-paid' ) . ' ' . esc_html( $transaction_id ) . '</h4></div>';
                }
            }
        }

        public function display_proof_file( $order ) {
            if ( 'momo-paid' === $order->get_payment_method() ) {
                $proof_url = get_post_meta( $order->get_id(), '_momo_proof_url', true );
                if ( $proof_url ) {
                    $file_ext = pathinfo( $proof_url, PATHINFO_EXTENSION );
                    echo '<div class="order_data_column"><h4>' . esc_html__( 'Preuve de paiement :', 'momo-paid' ) . '</h4>';
                    if ( in_array( $file_ext, array( 'jpg', 'jpeg', 'png' ) ) ) {
                        echo '<img src="' . esc_url( $proof_url ) . '" style="max-width:200px;"><br>';
                    }
                    echo '<a href="' . esc_url( $proof_url ) . '" target="_blank">' . basename( $proof_url ) . '</a></div>';
                }
            }
        }

        public function process_payment( $order_id ) {
            $order = wc_get_order( $order_id );
            $order->update_status( 'on-hold', __( 'Votre paiement doit être vérifié, merci de bien vouloir patienter.', 'momo-paid' ) );
            WC()->cart->empty_cart();

            return array(
                'result'   => 'success',
                'redirect' => $this->get_return_url( $order ),
            );
        }

        public function payments_scripts() {
            // wp_enqueue_style( 'momo-paid-styles', plugin_dir_url( __FILE__ ) . 'css/momo-paid-styles.css', array(), '1.0' );
        }
    }
}

function momo_paid_add_gateway_class( $methods ) {
    $methods[] = 'momo_paid_payment_gateway';
    return $methods;
}
add_filter( 'woocommerce_payment_gateways', 'momo_paid_add_gateway_class' );

function momo_paid_payment_using_mobile_solutions( $recipient_name, $recipient_number ) {
    global $woocommerce;
    $cart_total_price = $woocommerce->cart->get_total();

    echo sprintf( __( '</br><strong>1°)</strong>  <strong>COMPOSER LE CODE USSD MOMO MONEY DE VOTRE PAYS POUR RÉGLER </strong><h4><strong>%1$s</h4></strong> À <strong>%2$s</strong> <strong>AU</strong> : <strong><h4 style="background-color:#f7e859;"> %3$s</h4></strong>.<br/><br/> <strong>2°) VÉRIFICATION PAR RÉFÉRENCE</strong> <br/>
    * <strong>Cette vérification initiale requiert le numéro de référence envoyé </strong>  <strong>dans l'accusé de réception par votre opérateur mobile après votre transaction</strong>.', 'momo-paid' ), wp_kses_post( $cart_total_price ), esc_html( $recipient_name ), esc_html( $recipient_number ) );
}

Quelqu’un peut me dire quoi faire ?

PHPSpreadsheet: styling completely ignored

Trying to add styling to PHPSpreadsheet’ cells:

$spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        $irow = $icol = 1;
        foreach ([
                     'Operation type',
                     'Name',
                     'Status',
                     'Start',
                     'End',
                     'Duration'
                 ] as $title) {
            $sheet->setCellValue([$icol, $irow], $title);
            $sheet->getStyle([$icol, $irow])->getFont()->setBold(true);
            $sheet->getStyle([$icol, $irow])->getAlignment()->setVertical(Alignment::VERTICAL_TOP);
            $icol++;
        }
        //
        $writer = new Xlsx($spreadsheet);
        $writer->save('hello world.xlsx');

Finally, I see properly filled Excel sheet without any styling – no bold font, no vertical alignment.

What’s wrong?

UPDATE:

  1. getStyle accepts array of 4 ints instead of array of 2 ints in setCellValue:
    /**
     * Get style for cell.
     *
     * @param AddressRange|array<int>|CellAddress|int|string $cellCoordinate
     *              A simple string containing a cell address like 'A1' or a cell range like 'A1:E10'
     *              or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
     *              or a CellAddress or AddressRange object.
     */
    public function getStyle($cellCoordinate): Style
  1. getStyleByColumnAndRow marked as deprecated. Recommends to use getStyle instead

Saving a file using Google Drives API to a specific location

I have a PHP app that saves some information into a .CSV file. I want to upload it to my Google Drive.

This seems to upload the file:

$service = new Google_Service_Drive($client);

$fileMetadata = new Google_Service_Drive_DriveFile(array(
    'name'      => 'file.csv',
    'parents'    => [$FOLDER_ID]
));

$file = $service->files->create($fileMetadata, array(
    'data' => $product_data,
    'mimeType' => 'application/octet-stream',
    'uploadType' => 'multipart',
    'fields' => 'id'
));

This has some weird issues:

  1. To upload it to a specific folder, I have to know the FILE ID, as opposed to simply adding the path.

  2. It will continuously add files with the same name instead of overwriting the file.

After some research, I can use $service->files->update to overwrite the existing file. However, to do that, I would need to know the $file_id.

Is there a way to upload files using the path, as opposed to IDS?
Is there a way to create the file if it doesn’t exist, or overwrite it if it does?

I can only think of two ways to do this:

  1. Hardcode the file name and DIR ids, then update it if it ever changes.
  2. Write code that scans the drive to figure out if the folder and file ids

I am adapting from the Dropbox API, which comparatively was much more intuitive.

Any help is appreciated – thanks

PHP not releasing memory of variable

I have a server which has 2 PHP scripts that run continuously in the background. One of those scripts, for some reason, is not releasing memory and it’s making the other script fail because eventually I have no available RAM left on the server.

I created a pretty simple POC so you can check by yourself. Basically I have a function that checks the available RAM memory (using free command) three times, and after the first time I execute the function that makes memory be consumed and never released. I even tried gc_collect_cycles but nothing happens.

What amazes me, is that after the script finishes completely, if I go to my SSH and execute “free” all the used memory is indeed released however if I add a long sleep at the end o my script, the memory will keep being used. Notice that inside the function test() a local variable is created and it is explicitly unsetted, but for some reason PHP still holds the memory.

<?php

set_time_limit(0);
ini_set("memory_limit","-1");



function show_available_ram() {

    $temp1111 = shell_exec('sudo free -m');

    preg_match("/Mem: +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +([0-9]+)/",$temp1111,$temp2222);

    return $temp2222[1] . " MB";

}



function test() {

    $temp3333 = array();
    
    for ($i=0;$i<1000000;$i++) {
        
        $temp3333[] = array("key1" => md5($i), "key2" => $i);
        
    }

    unset($temp3333);

}



echo "nAAA: " . show_available_ram();

test();

sleep(2);

echo "nBBB: " . show_available_ram();

//I know it should not be needed the line below, but I am trying it anyway but it does not help.
gc_collect_cycles();

sleep(2);

echo "nCCC: " . show_available_ram();

?>

The code above outputs:

AAA: 1297 MB
BBB: 873 MB
CCC: 869 MB

So you can clearly see memory being used (on BBB and CCC but not released even after test() has already finished.

In the code above I use a few sleep so PHP can have time to garbage collect, but it does not help at all.

How to Remove “Contact Information” section from WooCommerce Checkout Blocks?

I’m using WooCommerce and trying to customize the checkout page. At the top, there’s a “Contact Information” section that includes a required Email Address field.

enter image description here

I want the checkout form to start directly from the “Billing Details”, without showing the email field or “Contact Information” section at all.

I tried using the following code in my functions.php file:

add_filter('woocommerce_checkout_fields', 'remove_billing_email_field');
function remove_billing_email_field($fields) {
    unset($fields['billing']['billing_email']);
    return $fields;
}

I also tried other similar code snippets from different sources, but none of them removed that section, and the email field still appears.

Additionally, I used the plugin Checkout Field Editor for WooCommerce by ThemeHigh. Even after removing the email field from the “Billing” block form, it still appears at the top under “Contact Information”.

Most tutorials and YouTube videos show the checkout starting directly from Billing Details, but in my case, this extra top section appears.

How can I properly remove or hide the Contact Information section, including the email field, from the WooCommerce Checkout Blocks page?

I tried both custom code snippets and plugin-based solutions. I used the Checkout Field Editor for WooCommerce by ThemeHigh and removed the email field from the Billing block, but it still shows up in the “Contact Information” section.

I expected that removing the email field from either the billing fields or the plugin interface would completely hide the entire top section (including its heading), and the checkout form would start directly from the “Billing Details” section — just like shown in most tutorials.

But none of the methods I’ve used actually removed that top section. It continues to appear, even with the email field supposedly removed.

AI Blog Title Generator [closed]

Below there is part of the code for AI blog title generator in wordpress I found online. This is causing an error and I suspect this is due to the variable $jsonData which has not been initialized and is being passed as an empty string. Anyone can grab the entire html and code snippet from https://www.youtube.com/watch?v=Pl25QeHVjvM&t=417s

function jsonToModel($modelClass, $jsonData) {
try {
    return $modelClass($jsonData);
} catch (Exception $e) {
    echo "Validation error: " . $e->getMessage();
    return null;
}
}
function validateJsonWithModel($modelClass, $jsonData) {
$validatedData = [];
$validationErrors = [];

if (is_array($jsonData)) {
    foreach ($jsonData as $item) {
        try {
            $modelInstance = $modelClass($item);
            array_push($validatedData, $modelInstance);
        } catch (Exception $e) {
            array_push($validationErrors, ["error" => $e->getMessage(), "data" => $item]);
        }
    }
} elseif (is_assoc($jsonData)) {
    try {
        $modelInstance = $modelClass($jsonData);
        array_push($validatedData, $modelInstance);
    } catch (Exception $e) {
        array_push($validationErrors, ["error" => $e->getMessage(), "data" => $jsonData]);
    }
} else {
    throw new ValueError("Invalid JSON data type. Expected associative array or array.");
}
return [$validatedData, $validationErrors];

}

How to spl_autoload_register multiple directories for same namespace

I have an existing autoload, which goes through json file to autoload different 3rd party files, this works really well. However there are new files being added to project and they are in different directories but using the same namespace. Issue is that it seems only to load the last directory for the same namespace, so when it goes to load the file it can’t find it.

I figured that would need to put the directories into an array and loop through that, but I am unsure how to go about that in PHP code.

Current PHP Code

spl_autoload_register(function (string $class_name): void {
    // Get mappings from json
    $conductor = file_get_contents(__DIR__.'/autoload.json');
    $conductor = str_replace('\', '\\', $conductor);
    $configuration = json_decode(stripslashes($conductor), true);

    // Map the namespace to the corresponding folder
    $namespace_mapping = $configuration['autoload']['psr-4'];
 
    foreach ($namespace_mapping as $namespace => $directory) {
        if (
            strpos($class_name, $namespace = trim($namespace, '\')) !== 0
            || (!$directory = realpath(__DIR__ . DIRECTORY_SEPARATOR . trim($directory, DIRECTORY_SEPARATOR)))
        ) {
            continue; // Class name doesn't match or the directory doesn't exist
        }
 
        // Require the file
        $class_file = $directory . str_replace([$namespace, '\'], ['', DIRECTORY_SEPARATOR], $class_name) . '.php';
        if (file_exists($class_file)) {
            require_once $class_file;
        }
    }
});

Current Json

{
    "autoload": {
        "psr-4": {
            "Psr\Log\": "psr/log/src/",
            "Psr\Http\Server\": "psr/http-server-handler/src/",
            "Psr\Http\Server\": "psr/http-server-middleware/src/",
            "Psr\Http\Message\": "psr/http-message/src/",
            "Psr\Http\Message\": "psr/http-factory/src/",
            "Psr\Container\": "psr/container/src/"
        }
    }
}

I was thinking needing to do something like this in json:

"Psr\Http\Server\": ["psr/http-server-handler/src/", "psr/http-server-middleware/src/"],

Any assistance would be greatly appreciated.

fix the ranking issue [closed]

This is my code where I am getting country ranking on corruption by get request

public function getByYear($year)
    {
        return $this->safeCall(function () use ($year) {
            $rankings = CorruptionRanking::where('year', $year)
                ->orderBy('points', 'desc')
                ->get();

            if ($rankings->isEmpty()) {
                return $this->errorResponse('No data found for the given year', 404);
            }
            $rank = 1;
            $rankings->each(function ($ranking, $index) use (&$rank, $rankings) {
                if ($index > 0 && $ranking->points === $rankings[$index - 1]->points) {
                    $ranking->rank = $rank -1;
                } else {
                    $ranking->rank = $rank;
                    $rank++;
                }
                $ranking->save();
            });
            return $this->successResponse('Rankings retrieved successfully', [
                'year' => $year,
                'rankings' => $rankings
            ]);
        });
    }

And this is my output

"rankings": [
            {
                "id": 1541,
                "country_name": "Denmark",
                "year": 2017,
                "points": 8.8,
                "rank": 1,
                "created_at": "2025-04-17T11:41:36.000000Z",
                "updated_at": "2025-04-19T05:37:40.000000Z"
            },
            {
                "id": 1886,
                "country_name": "Finland",
                "year": 2017,
                "points": 8.5,
                "rank": 2,
                "created_at": "2025-04-17T11:41:36.000000Z",
                "updated_at": "2025-04-19T05:38:37.000000Z"
            },
            {
                "id": 3680,
                "country_name": "Norway",
                "year": 2017,
                "points": 8.5,
                "rank": 2,
                "created_at": "2025-04-17T11:41:38.000000Z",
                "updated_at": "2025-04-19T05:38:37.000000Z"
            },
            {
                "id": 4715,
                "country_name": "Switzerland",
                "year": 2017,
                "points": 8.5,
                "rank": 2,
                "created_at": "2025-04-17T11:41:39.000000Z",
                "updated_at": "2025-04-19T05:38:37.000000Z"
            },
            {
                "id": 4370,
                "country_name": "Singapore",
                "year": 2017,
                "points": 8.4,
                "rank": 3,
                "created_at": "2025-04-17T11:41:39.000000Z",
                "updated_at": "2025-04-19T05:38:37.000000Z"
            },

But i am expecting output where Denmark rank 1 its good and Finland,Norway,Switzerland rank 2 is also good But Singapore,Sweden should be rank 5 and this will follow the ranking sequence thanks

Why does phpunit’s check for the passed arguments fail when I use named arguments?

I have a simple test where I mock a dependency of the code under test:

<?php

use PHPUnitFrameworkTestCase;

class Dependency {
    public function someFunction(int $first, int $second): int {
        return $first + $second;
    }
}

class CodeUnderTest {
    public function testMe(Dependency $dep, int $first, int $second): int {
        return $dep->someFunction($first, $second);
    }
}

class TheTest extends TestCase
{
    public final function testMe(): void {
        $dep = $this->createMock(Dependency::class);
        $dep->expects(self::once())->method('someFunction')->with(second: 2, first: 1)->willReturn(3);
        $codeUnderTest = new CodeUnderTest();
        $codeUnderTest->testMe($dep, 1, 2);
    }
}

This fails:

Expectation failed for method name is "someFunction" when invoked 1 time(s)
Parameter 0 for invocation Dependency::someFunction(1, 2): int does not match expected value.
Failed asserting that 1 matches expected 2.
Expected :2
Actual   :1

Why does this happen? My mock object clearly defines the expected values, even by name.