Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Jmrtech's avatar

relationship not loading

Hi, laravel is not loading the relationship, and I'm not sure why. On all other relationships it doesn't do this on. I have paymentmethod which belongs to a billingaddress, and billingaddress can have many paymentmethods. The tables are named billing_addresses and payment_methods. Here is the query that is being executed:

"query" => "select * from `billing_addresses` where billing_addresses.hospital_id = '3' and `billing_addresses`.`id` is null limit 1"

PaymentMethod:

public function billingAddress ()
    {
        return $this->belongsTo('App\BillingAddress');
    }

BillingAddress:

public function paymentMethods()
    {
        return $this->hasMany('App\PaymentMethod');
    }

Normally I could say: $paymentmethod->billingAddress_id->zipcode and it would bring it up. Hopefully someone can see something I may have overlooked.

0 likes
2 replies
Jmrtech's avatar

Sorry, forgot about that

Schema::create('payment_methods', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('cardholderName',175);
            $table->string('billingAddress_id');
            $table->string('token',35)->unique();
            $table->string('cardType');
            $table->string('imageUrl');
            $table->string('maskedNumber');
            $table->string('lastFour');
            $table->string('expirationMonth');
            $table->string('expirationYear');
            $table->integer('hospital_id')->unsigned();
            $table->timestamps();

            $table->foreign('hospital_id')
                ->references('id')
                ->on('hospitals')
                ->onDelete('cascade');

            $table->foreign('billingAddress_id')
                ->references('id')
                ->on('billing_addresses')
                ->onDelete('cascade');

        });
Schema::create('billing_addresses', function(Blueprint $table)
        {
            //$table->increments('id');
            $table->string('id',2)->primary();
            $table->integer('hospital_id')->unsigned();
            $table->string('firstName');
            $table->string('lastName');
            $table->string('company');
            $table->string('streetAddress');
            $table->string('extendedAddress');
            $table->string('locality');
            $table->string('region');
            $table->string('postalCode');
            $table->string('countryCodeAlpha2');
            $table->timestamps();

            $table->foreign('hospital_id')
                ->references('id')
                ->on('hospitals')
                ->onDelete('cascade');

        });

Please or to participate in this conversation.