In this case, it would be best to use a polymorphic table to store the shared content. This will allow for more flexibility in the future if additional sharing options are added.
The table could be named something like "content_shares" and have columns for the shared content's ID, the type of sharer (user, group, or position), and the ID of the sharer.
Here's an example migration for the "content_shares" table:
Schema::create('content_shares', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('content_id');
$table->string('sharer_type');
$table->unsignedBigInteger('sharer_id');
$table->timestamps();
$table->foreign('content_id')->references('id')->on('content');
$table->index(['sharer_id', 'sharer_type']);
});
Then, to retrieve the shared content for a user, group, or position, you can use a polymorphic relationship. For example, to get all content shared with a user:
class User extends Model
{
public function sharedContent()
{
return $this->morphedByMany(Content::class, 'sharer', 'content_shares');
}
}
$user = User::find(1);
$sharedContent = $user->sharedContent;