5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravelでいいね機能

Last updated at Posted at 2019-07-24

環境

PHP 7.3
Laravel 5.8

Laravelにいいね機能を実装しようと思い、調べていたらLaravel-LoveというライブラリがLaravel Newsで紹介されていました。

cybercog/laravel-love

準備

今回はUserモデルでPostモデルにいいねします。

インストール

$ composer require cybercog/laravel-love
$ php artisan migrate

Likeタイプを生成

$ php artisan love:reaction-type-add Like 1

Reactarebl(いいねするモデル)の準備

app/User.php
<?php

namespace App;

use Cog\Contracts\Love\Reacterable\Models\Reacterable as ReacterableContract;
use Cog\Laravel\Love\Reacterable\Models\Traits\Reacterable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements ReacterableContract
{
    use Reacterable;
}
$ php artisan love:setup-reacterable --model="App\User" --nullable

Reactable(いいねされるモデル)準備

app\Post.php
<?php

namespace App;

use Cog\Contracts\Love\Reactable\Models\Reactable as ReactableContract;
use Cog\Laravel\Love\Reactable\Models\Traits\Reactable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model implements ReactableContract
{
    use Reactable;
}
$ php artisan love:setup-reactable --model="App\Post" --nullable

実際にいいねする

<?php
use App\User;
use App\Post;
use Cog\Laravel\Love\ReactionType\Models\ReactionType;

$user1 = User::find(1);
$post1 = Post::find(1);

$likeType = ReactionType::fromName('Like');

//リアクター、リアクタントとして登録。
//すでに登録してある場合、例外が投げられます。
if ($user1->isNotRegisteredAsLoveReacter()) {
    $user1->registerAsLoveReacter();
}
if ($post1->isNotRegisteredAsLoveReactant()) {
    $post1->registerAsLoveReactant();
}

$reacter = $user1->getLoveReacter();
$reactant = $post1->getLoveReactant();

//$user1が$post1をいいねしていないことを確認
if ($reacter->isNotReactedToWithType($reactant, $likeType)) {
    //$user1で$post1をいいね
    $reacter->reactTo($reactant, $likeType);
}

//$user1がいいねしたPostが入ったCollectionを取得
$posts = Post::query()
    ->whereReactedByWithType($reacter, $likeType)
    ->get();

//$post1をいいねしたUserが入ったCollectionを取得
$likeReactions = $post1
    ->getLoveReactant()
    ->reactions()
    ->where('reaction_type_id', ReactionType::fromName('Like')->getId())
    ->get();

$likeUsers = [];
foreach ($likeReactions as $likeReaction) {
    $likeUsers[] = User::where('love_reacter_id', $likeReaction->getReacter()->id)->first();
}

//$post1をいいねした数を取得
count($likeUsers);

//いいね解除
if ($reacter->isReactedToWithType($reactant, $likeType)) {
    $reacter->unreactTo($reactant, $likeType);
}

参考

Laravel-Loveのドキュメンテーション
Githubのissue

5
5
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?