lundi 5 mars 2018

Attach function not working in Eloquent,

Code that is executed when I like a post

The attach function does not work when I use this line.

$post->post_likes()->attach(auth()->id());

But it works when I use this line.

$post->like()->attach(auth()->id());

-

Post Model

class Post extends Model
{

    public function post_likes()
    {
        // The attach method should work because a post does have many likes.
        return $this->hasMany(PostLike::Class);
    }

    public function like() {
        // I don't think this code should belong in the Post model, but it works.
        return $this->belongsToMany(User::Class, 'post_likes', 'post_id', 'user_id')->withTimestamps();
    }

}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2oOk3Uy
via IFTTT

Aucun commentaire:

Enregistrer un commentaire