0

I'm currently working on a feature where when a user is created his job position can be selected and I connected that through a foreign key where the user gets to select the ID of the specific job and it works well when storing the data, the actual ID of the specific field is showing on the users table but now my problem is when I want to show that specific data of the authenticated user Here's the table I have

Job's Table

  1. id
  2. job_name
  3. line_manager
  4. department
  5. branch
  6. job_type

The id is a foreign key for the users table at

  1. job_id

Heres my JobController:

public function index()
    {
        $role = Auth::guard('web')->user()->role_id;
        if($role == 1)
        {
            $jobs = Job::latest()->where('id',auth()->user()->id)->get();
            return view('job.index',compact('jobs'));
        }

The Model:

class Job extends Model
{
    protected $table = 'jobs';

    public function jobWhich()
    {
        return $this->job_name;
    }

And this is the view I want the data of that ID that I selected when I created the user to show here(its static data that was inserted in the database so it won't change)

<tbody>
@foreach($jobs as $job)
<tr>
<td>{{$job->job_name}}</td>
<td>{{$job->line_manager}}</td>
<td>{{$job->department}}</td>
<td>{{$job->branch}}</td>
<td>{{$job->job_type}}</td>
</tr>
@endforeach
</tbody>

I'm not sure if I need to show the users controller on how I store this into the user but it's working well because I can see the id of the specific field at the user when I create

2
  • perhaps you would like to setup a relationship between the User and Job model so you can achieve this easily? Commented Jul 29, 2020 at 11:01
  • The user has a foreign key that references the id of the jobs so maybe that would work? But im rather new so im unsure on how to do that all i need to do is just display the data from the field of that id that the user has on the job_id from when i created it Commented Jul 29, 2020 at 11:03

4 Answers 4

2

User Model

public function job(){
   return $this->belongsTo(App\Job::class, 'user_id', 'job_id');
}

Job Model

public function users(){
   return $this->hasMany(App\User::class, 'job_id', 'user_id');
}

Controller

$user = Auth::user();
// In cas you want to eager load.
$user->load('job');

You can access the user's job using the job relation.

   echo $user->job->job_name;

Sign up to request clarification or add additional context in comments.

Comments

2

You need a relation between user and job. Just add created_by column in jobs table/migration. then use this relation to the job model:

class Job extends Model
{
    
    public function creator()
    {
        return $this->belongsTo(User::class,'created_by');
    }
}

and in your controller use this:

$jobs = Job::latest()->where('created_by',auth()->user()->id)->get();

Comments

2

You can define a relationship on the User model for job to make this easier:

class User ...
{
    public function job()
    {
        return $this->belongsTo(Job::class);
    }
}

Then you can simply get the user's job via the relationship:

$job = Auth::guard('web')->user()->job;

Laravel 7.x Docs - Eloquent - Relationships - One to Many (inverse) belongsTo

Comments

1

If I understand you correctly you want to show the job of the user.

If this is correct you will need to set up a foreig key in your User Model.

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

Then you are able to get the users job like: $job = $user->job or $user->job()->get().

Afterwards you can pass it like that to your view (as you did with all the jobs): return view('job.index',compact('job'));

At last you won't need to use a foreach loop in your blade: You can simply display the items like `{{$job->job_name}}? n and so on.

Let me know if this is what you mean

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.