requincreative's avatar

Model Relationship Clarification

Greetings,

I have painted myself into a corner with a few model relationships and it has come to my attention that I am really not fully comprehending the nature of all available relationship types so I am posting here hoping that someone can help me make sense of it.

I have an app that has users, processes and I am trying to create a relationship that returns the users who are allowed to access a process so that I only show those users when I edit a process.

NOTE: I know that I am not following the expected paradigm for pivots and relationships by specifying keys other than the id fields between the User and the BC_User models, but this is my test environment and I do not have access to the users table in the same SQL database schema so I am using another connection.  This will be resolved when the application is pushed to production.

I have three Models

App\User
App\BC_User
App\BC_Process

Here are the migrations for each along with a pivot table for BC_Process_User'


Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('campus');
            $table->string('office');
            $table->string('department');
            $table->string('title');
            $table->string('ein');
            $table->integer('role')->default(1);
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
 Schema::create('BC_Process', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('Process');
            $table->unsignedBigInteger('user_id');
            $table->timestamps();
        });

// Pivot
 Schema::create('BC_Process_User', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('process_id');
            $table->unsignedBigInteger('user_id');
            $table->integer('SchoolYr');
            $table->timestamps();
        });

Schema::create('BC_User', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->integer('ein');
            $table->timestamps();
 });

Here are my models

class User extends Authenticatable
{
 public function bcuser()
    {
        return $this->hasOne('AppBC_User', 'ein', 'ein');
    }
}

class BC_User extends Model
{
public function user()
    {
        return $this->belongsTo('App\User', 'ein', 'ein');
    }

    public function allowed()  // allowed processes
    {

        return $this->belongsToMany('App\BC_Process', 'BC_Process_User', 'process_id', 'user_id')
            ->withPivot('SchoolYr')
            ->withTimestamps();
    }

}

class BC_Process extends Model
{
 public function user()
    {
        return $this->belongsTo('App\User');
    }


    public function bcusers()
    {

        return $this->belongsToMany('App\BC_User', 'BC_Process_User', 'process_id', 'user_id')
            ->withPivot('SchoolYr')
            ->withTimestamps();

      
    }

}

Basically I am trying to return the allowed processes for the currently logged in user with the following

dd(auth()->user()->bcuser()->allowed()->get());

The above code returns the following error:

BadMethodCallException
Call to undefined method Illuminate\Database\Eloquent\Relations\HasOne::allowed()
0 likes
3 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The error is occurring because the allowed() method is defined in the BC_User model, but you are trying to access it through the bcuser() relationship in the User model, which is a hasOne relationship.

To fix this, you need to update the bcuser() relationship in the User model to be a belongsTo relationship instead of hasOne. Here's the updated code:

class User extends Authenticatable
{
    public function bcuser()
    {
        return $this->belongsTo('App\BC_User', 'ein', 'ein');
    }
}

Now, you can access the allowed() method through the bcuser() relationship. Here's the updated code:

dd(auth()->user()->bcuser->allowed()->get());

This should resolve the error and return the allowed processes for the currently logged in user.

1 like

Please or to participate in this conversation.