scott87's avatar

Getting user name from pivot table

I am trying to obtain the username from the pivot table job_step below

Imgur

I have relationships already built between Job and Step models but unsure how to obtain anything other than the user_id from the pivot table as I can't create a relationship in the same way.

Any suggestions?

from Job.php

 public function steps()
    {
        return $this->belongsToMany(Step::class)
            ->withPivot([
                'step_id', 'job_id', 'status', 'image_src', 'user_id', 'notes', 'completed_at'
            ]);
    }
0 likes
6 replies
s3r0s4pi3ns's avatar

I guess that 'Job.php' is not related to Queue concept (correct me if I am wrong). From my point of view, your job_step pivot table does not need the user_id because you already have that information through the job.

A good way is to create a model that represents the pivot table JobStep, this comes with extra benefits like dispatch events when a job_step is created or deleted, define computed attributes, relationships, etc.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use App\Models\User;

class JobStep extends Pivot
{
    protected $table = 'job_step';

    protected $fillable = ['job_id', 'step_id', //...];

// This is not necessary, just to give an extra point in case you need it
    protected $dispatchesEvents = ['created' => YourCustomEvent::class];

    public function job(): BelongsTo
    {
        return $this->belongsTo(Job::class)->with('user');
    }

	public function user(): User {
		return $this->job->user;		
	}

}

So once this is created you can link this class with the method using() on your belongsToMany Relationship

public function steps()
    {
        return $this->belongsToMany(Step::class)
			->using(JobStep::class)
            ->withPivot([
                'step_id', 'job_id', 'status', 'image_src', 'user_id', 'notes', 'completed_at'
            ]);
    }

// You'll see something similar in the Models retrieved that contains the pivot
App\Models\Step {#4889
//...
 pivot: App\Models\JobStep {#4888
      step_id: 5193471413587968,
      job_id: 5193470755082240,
//...
	}
}
//And you can access like this via the many-to-many relationship:
$step->pivot->user()->name

I hope that was clear because many to many relationships can be a bit tricky when you are starting out but once you understand them everything is clear.

1 like
scott87's avatar

@s3r0s4pi3ns

I think this seems to be what i need, thank you.

I do still need the user_id for job_step as the user completing the job_step task will be different from the user creating the initial job (if that makes sense?)

I was just unsure weather i’d need to have a separate jobStep model.

Thanks again, will use this approach.

scott87's avatar

@s3r0s4pi3ns

just tried this out and all I am receiving is the 'user_id' from the jobs table, I need the 'user_id' from the job_step table.

$step->pivot->user()->name 
s3r0s4pi3ns's avatar
Level 2

@scott87 Well, I think there is two approachs for this:

  • Accessing the raw value with $step->pivot->user_id if you only want the id value but not the entire User model
  • Change the relationship of user() from JobStep model to instead retrieve the user model from the pivot user_id value:
public function user(): User {
		return $this->belongsTo(User::class, 'user_id', 'id');		
	}
1 like

Please or to participate in this conversation.