Dronax
229
4
Laravel

Belongs to many not attaching data Laravel

Posted 3 months ago by Dronax

I have two tables: reviews and guest_reviews. In table reviews I have columns: id, post_id, user_id, body,parent_id. In table reviews_guests (is pivot table) I have columns: review_id, guest_name, guest_email, guest_token.

In model review I have this relation pivot:

public function guest()
{
    return $this->belongsToMany(static::class, 'reviews_guests');
}

When I create review with guest, method attach not working:

public function createReview($data = [], $guest = [])
{
    if ($guest) {
        $review = $this->reviews()->create($data);
        $review->guest()->attach($guest);
        $review->save();

        return $review;
    } else {
        $review = $this->reviews()->create($data);
        return $review;
    }

}

Why attach not working? On array guest and data I have data. In table reviews review is created, but in reviews_guests - not.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.