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

Brandon.css's avatar

Question & answer website timeline Laravel 5.5 Eloquent Help

I'm currently building a small app to learn Laravel. I'm new to the whole framework and I've been struggling with creating a timeline...

In the app you can ask a user a question, and that user can answer the question. You can also follow users and on the homepage/timeline, you can view everyone's questions/answers.

I have all this setup and working.

However, there are a few issues:

  • On the timeline, I'd like to show everyone's questions/answers of user I'm following plus my own.
  • Make sure only answered questions are shown
  • And then sort the posts in order by the answer's created_at.

On the timeline I have this:

$id = Auth::id();
$replies = Questions::has('answer')->with('user')
                ->join('followers', 'followers.user_id', '=', 'questions.user_id')
                ->where('followers.follower_user_id', '=', $id)
                ->orderBy('followers.created_at', 'desc')->get();

Here are my Models:

Answer.php

public function question()
{
    return $this->belongsTo('App\Questions', 'question_id');
}

Questions.php

 // returns the answer instance for the questions
public function answer()
{
    return $this->hasOne('App\Answer', 'question_id');
}

// returns the user who was asked the question
// i.e billy asked Fred 'who is your crush', Fred is returned
public function user()
{
    return $this->belongsTo('App\User', 'user_id');
}

// returns the user who asked the question
// i.e billy asked Fred 'who is your crush', Billy is returned
public function author()
{
    return $this->belongsTo('App\User', 'author_id');
}

User.php

public function questions()
{
    return $this->hasMany('App\Questions');
}

/**
* The following that belong to the user.
*/
public function following()
{
    return $this->belongsToMany('App\User', 'followers', 'follower_user_id', 'user_id')->withTimestamps();
}

public function isFollowing(User $user)
{
    return ! is_null($this->following()->where('user_id', $user->id)->first());
}

I have 3 tables:

  • Users
  • Followers,
  • Posts

The Tables look like this:

  • Users: id
  • Followers: user_id, follower_user_id (user_id is the logged in user, follower_user_id is the user the logged in user is following)
  • Posts: user_id

Is there a way to acomplish this with the way I'm currently doing it, or is this not going to work? I was going to make a bunch of foreach loops and add all the data to an array and pass the array to my view but figured that would be a terrible idea.

Thanks for all the help, really appreciate it!

0 likes
2 replies
slashequip's avatar

Hey @Brandon.css,

I think you are on the right kind of track with the foreach & arrays BUT this isn't very OOP. So I'd go about creating a specific 'Timeline' class to handle/store your questions (and answers). You'll need a method for adding (either single, multiple or both) questions then you can have a method for retrieving them, which also orders them by their timestamp.

It might also be good to have a Repository for retrieving questions this way you could use Dependancy Injection in the constructor of your 'Timeline' class.

Hopefully that makes sense? This should give you flexibility in the future should you need to get more questions from another source and add them into your 'Timeline'.

Brandon.css's avatar

So I have setup the timeline by merging two collections and added a last_answered_at column to my Questions table and add a timestamp to that col whenever the answer to that questions gets created. This helps me easily be able to sort all my questions for the timeline,

My only problem going forward is how would I add pagination to my timeline?

$follow_replies = Questions::has('answer')->with('user')
                ->join('followers', 'followers.user_id', '=', 'questions.user_id')
                ->where('followers.follower_user_id', '=', $id)
                ->orderBy('questions.last_answered_at', 'desc')->get();

$follow_replies = collect($follow_replies);

$user_replies = collect(Questions::has('answer')->where('user_id', $id)->get());

$replies = $user_replies->merge($follow_replies)->sortByDesc('last_answered_at');

Please or to participate in this conversation.