A better approach would be to use a pivot table to store the many-to-many relationship between users and files. You can create a pivot table called file_user with columns file_id and user_id. This table would store a row for each user that has been granted permission to view a file.
Here's an example of how you could set up the pivot table and model relationships in your Laravel application:
// File model
class File extends Model
{
public function users()
{
return $this->belongsToMany(User::class, 'file_user');
}
}
// User model
class User extends Authenticatable
{
public function files()
{
return $this->belongsToMany(File::class, 'file_user');
}
}
With this setup, you can easily query the pivot table to determine which users have permission to view a given file. For example, to get all users that have permission to view a file with ID 1:
$file = File::find(1);
$users = $file->users;
Alternatively, you can also query the pivot table directly:
$users = User::whereHas('files', function($query) {
$query->where('file_id', 1);
})->get();