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

damian0021's avatar

Relationship between three tables - additional information

I have a problem in Laravel.

I have 3 models:

Thread - fuser_id Post - thread_id, fuser_id User - id

I want to get the last post that was created for a given thread and information about the user who wrote it.

Model Thread:

public function lastpost()
{
		return $this->hasOneThrough(Post::class, User::class, 'id', 'fuser_id', 'fuser_id', 'id);
}

Controller:

$threads = Thread::where('category_id', $categories->id)->with('lastPosts')->get();

Receives user ID information, but needs more information, which is in the database.

0 likes
3 replies

Please or to participate in this conversation.