Laravel: Best way to have multiple user relations on same model

Viewed 49

I have a polling app.

Admins can create new polls. I have a belongsTo relation in my polls Model so I can later track which user created the poll.

Now I want to allow the poll admin to add users from the list of registered users so that only these users can access the poll.

Adding another hasMany relation to users to the poll model does not work because it already has the belongsTo. But what would the best way be? Simply add a field to my poll model and fill it with a list of user ids? Auth? But does that make sense for possibly hundreds of polls?

I feel like I am not seing an obvious solution here...

Thanks

Poll Model

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Poll extends Model
{
    use HasFactory;

    protected $guarded = [];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

}

User model

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, HasRoles;

     ...

    /**
     * poll relationship
     */

    public function polls()
    {
        return $this->hasMany(Poll::class);
    }
}

Poll migration

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('polls', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->string('name');
            $table->timestamp('start_time')->nullable();
            $table->timestamp('end_time')->nullable();
            $table->timestamps();

            $table->index('user_id');
        });
    }
}

User migration

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }
}
1 Answers

You might want to create a a different, many to many relationship between polls and users. I call it participants.

table poll_user
    poll_id
    user_id

class Poll extends Model
{
    public function participants()
    {
        return $this->belongsToMany(User::class, 'poll_user');
    }
}

Related