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

tjhodge's avatar

Get $item->id from URL and use in next controller.

Hello! I have 3 models and the related tables in my database

User    (user_id)
Work Record (work_id, user_id, notes)
Job (job_id, work_id, description)

What I want to be able to do is visit "http://example.com/WorkRecords/{Work Record Id}/jobs" and then display all jobs/CRUD linked to the work record.

So far I can Create and Update Work Records linked to the user, but I am not too sure what happens next. I have

Route::resource('work-record/{id}/job', 'JobController');
Route::resource('work-record', 'WorkRecordController');

I am not too sure what to put in the JobController to get the work-record id and link into the jobs. Am I heading in the right direction?

Thanks in advance.

0 likes
2 replies
InaniELHoussain's avatar
Level 32

In your JobController

public function show($id){
    $workRecord = WorkRecord::findOrFail($id);
    $jobsRelated = $workRecord->jobs();
}
1 like
tjhodge's avatar

Thanks InaniElHoussain,

I had to add ->get() to the end of ->jobs() but worked a treat!

1 like

Please or to participate in this conversation.