Hi @dglaser2 Assuming you have defined the relationship of Post, Repost and User eloquent models, you should have something like this
In app/Models/Post.php
public function reposts(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Repost::class);
}
And in app/Models/Repost.php
public function post(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Post::class);
}
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(User::class);
}
Now you if you want to get the reposts of a certain post you can easily do
$post = \App\Models\Post::first();
// to access the user who reposted
$spot->reposts->first()->user->name;
And you better use eager loading to reduce the number of executed queries
$post = \App\Models\Post::with('reposts.user')->first();
// to access the user who reposted
$spot->reposts->first()->user->name;