vendredi 10 janvier 2020

Laravel Many to Many attach?

I'm trying to create or attach data to table called "user_votes", but I dunno why the connections between "Vote" and "User" doesn't work.

user_votes table

Schema::create('user_votes', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('user_id')->unsigned();
        $table->bigInteger('vote_id')->unsigned();
        $table->string('name');
        $table->string('page', 100)->nullable();
        $table->timestamps();

        $table->foreign('user_id')->references('id')->on('users');
        $table->foreign('vote_id')->references('id')->on('votes');
    });

Vote model:

public function user_votes()
{
    return $this->belongsToMany(User::class, 'user_votes');
}

User model:

public function user_votes()
{
    return $this->belongsToMany(Vote::class, 'user_votes');
}

store controller:

auth()->user()->user_votes()->attach([
            'name' => $option->name,
            'page' => $option->page
        ]);

Thanks for any ideas.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire