weixin_39844590 2020-11-22 01:42
浏览 0

Refactor package relations

I decided to revise and extend relationships in v6. Initially was planned to do it in v5 but it will require database changes and Laravel 5.6 morphTo implementation.

There is a need to fetch:

  1. Likeables of the Liker.
  2. Likers of the Likeable.
  3. (?) Likes which were added to Likeables.
  4. (?) Likes which were added by Liker.

I'm not 100% sure about points 3 & 4. Do we really need to get those likes? Need to write use cases for them.

Old implementation

Only 3rd point is implemented right now.

php
// Likes and dislikes related to Likeable
$likesAndDislikes = $article->likesAndDislikes();

// Likes related to Likeable
$likes = $article->likes();

// Dislikes related to Likeable
$dislikes = $article->dislikes();

该提问来源于开源项目:cybercog/laravel-love

  • 写回答

9条回答 默认 最新

  • weixin_39844590 2020-11-22 01:42
    关注

    1. Likeables of the Liker

    Option №1

    Liker model will have relationships likedAndDisliked, liked, disliked which returns Likeable models they liked or disliked.

    php
    // Likeables liked or disliked by user
    $likeables = $user->likedAndDisliked();
    
    // Likeables liked by user
    $likedLikables = $user->liked();
    
    // Likeables disliked by user
    $dislikedLikeables = $user->disliked();
    

    Drawback: There is no clear understanding that it's HasMany relationship. Name should be plural.

    评论

报告相同问题?