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

artisticre's avatar

Need Help With Relationships...

I have three tables, User, Profile, sponsorapp. I am struggling to connect all three with relationships. A User can have one profile but many sponsorapps.

User Table

 public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

Profile Table

public function up()
    {
        Schema::create('profile', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('user_id')->nullable();
            $table->string('address')->nullable();
            $table->string('apt')->nullable();
            $table->string('city')->nullable();
            $table->string('state')->nullable();
            $table->string('zipcode')->nullable();
            $table->string('homephone')->nullable();
            $table->string('cellphone')->nullable();
             $table->timestamps();
        });
    }

Sponsorapp

public function up()
    {
        Schema::create('sponsorapp', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('user_id')->unsigned();
            $table->string('besttime');
            $table->string('applicantname');
            $table->boolean('applicantbaptized');
            $table->boolean('spousediscuss');
            $table->boolean('bothspouseattend');
            $table->text('ifnospouseattend')->nullable;
            $table->text('whygoodcandidate');
            $table->text('applicantattitude');
            $table->text('leadershipexpectations');
            $table->text('sponsorsupport');
            $table->text('otherinfo');
            $table->date('datesigned');
            $table->timestamps();
        });
    }
0 likes
3 replies
MichalOravec's avatar
Level 75

User model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The roles that belong to the user.
     */
    public function profile()
    {
        return $this->hasOne('App\Profile');
    }

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

Profile model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    protected $table = 'profile';

    /**
     * Get the post that owns the comment.
     */
    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

Sponsorapp model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Sponsorapp extends Model
{
    protected $table = 'sponsorapp';

    /**
     * Get the post that owns the comment.
     */
    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

https://laravel.com/docs/7.x/eloquent-relationships#one-to-one

https://laravel.com/docs/7.x/eloquent-relationships#one-to-many

artisticre's avatar

When I do that I get Call to undefined relationship [sponsorapp] on model [App\User].

        $sponsor_print = User::with(['sponsorapp','profile'])->get();

MichalOravec's avatar

I set relationship as plural sponsorapps

$sponsor_print = User::with(['sponsorapps','profile'])->get();

Please or to participate in this conversation.