I am making a site where users can like books and also comment on them. I want the user to be able to comment only if it has liked at least 3 books. How can I implement this?

What is this kind of validation called because I am having a hard time searching for solutions.

Thanks!

This is how my code is build up:

User Model:

```
class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'role',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

    public function favorites()
    {
        return $this->belongsToMany(Book::class, 'favorites')
            ->using(Favorite::class)
            ->withTimestamps()
            ->as('favorites');
    }
}

```

User Controller:

```
class UserController extends Controller
{
    public function favorites(Request $request)
    {
        $books = $request->user()->favorites()->latest()->get();

        return true;
    }

}

```

Comment Model:

```
class Comment extends Model
{
    protected $fillable = ['body', 'book_id', 'user_id'];

    public function book()
    {
        return $this->belongsTo(Book::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function parent()
    {
        return $this->belongsTo(Comment::class, 'parent_id');
    }
}

```

Comment Controller:

```
class CommentsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function store(Request $request, Book $book)
    {
        $validated = $request->validate([
            'body' => 'required',
        ]);

        $book->comments()->create([
            'user_id' => auth()->id(),
            'body' => $validated['body'],
        ]);

        return back();
    }
}

```

Favorite Model:

```
class Favorite extends Pivot
{
    protected $table = 'favorites';

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function book()
    {
        return $this->belongsTo(Book::class);
    }
}

```

Favorite Controller:

```
class FavoritesController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        /** @var \App\Models\User $user */
        $user = Auth::user();

        $favorites = $user->favorites()
            ->orderByDesc('favorites.created_at')
            ->paginate(10);

        return view('books.favorites', ['favorites' => $favorites]);
    }

    public function update(Book $book)
    {
        /** @var \App\Models\User $user */
        $user = Auth::user();

        if ($user->favorites()->where('book_id', $book->id)->exists()) {
            return redirect()->back()
                ->with('error', 'This item is already in your favorites!');
        }
        $user->favorites()->attach($book);

        return redirect()->back()
            ->with('success', 'Book, ' . $book->title . ' Added to your reading list.');
    }

    public function destroy(Book $book)
    {
        /** @var \App\Models\User $user */
        $user = Auth::user();
        $user->favorites()->detach($book);

        return redirect()->back()
            ->with('success', 'Book removed from favorites');
    }
}

```

show.blade.php :

```
@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="my-3">
            <a class="btn btn-secondary" href="{{ route('books.index') }}">
                terug
            </a>
        </div>

        <div class="row my-3">
            <div class="col-lg">
                <img src="{{ $book->image }}" alt="{{ $book->title }}">
            </div>

            <div class="col-lg">
                <header>
                    @if($book)
                        <h1>{{$book['title']}}</h1>
                    @else
                        <h1>{{$error}}</h1>
                    @endif
                </header>

                <p>{{$book['author']}}</p>
                <p>{{$book['description']}}</p>

                @if($isFavorite)
                    <form method="POST" action="{{ route('favorites.destroy', ['book' => $book]) }}">
                        @csrf
                        @method('DELETE')

                        <button type="submit" class="btn btn-danger btn-block">Unfavorite</button>
                    </form>
                @else
                    <form method="POST" action="{{ route('favorites.update', ['book' => $book]) }}">
                        @csrf
                        @method('PUT')

                        <button type="submit" class="btn btn-primary btn-block">Voeg toe aan leeslijst</button>
                    </form>
                @endif

                @can('admin')
                    <a class="btn btn-secondary btn-block mt-2" href="{{ route('books.edit',$book->id) }}">
                        Edit
                    </a>

                    <form class="mt-2" action="{{ route('books.destroy', $book->id)}}" method="POST"
                          onsubmit="return confirm('Are you sure?')">
                        @csrf
                        @method('DELETE')
                        <button class="btn btn-danger btn-block" type="submit">Delete</button>
                    </form>
                @endcan
            </div>
        </div>

        <hr>

        @if($book->comments->isNotEmpty())
            <div class="card mb-3">
                <ul class="list-group list-group-flush">
                    @foreach ($book->comments as $comment)
                        <li class="list-group-item">
                            <p class="card-text">{{ $comment->body }}</p>

                            <p class="m-0 card-text small text-muted">
                                {{ $comment->created_at->toFormattedDateString() }}
                                &bull;
                                {{ $comment->user->name }}
                            </p>
                        </li>
                    @endforeach
                </ul>
            </div>
        @endif

        <form method="POST" action="{{ route('books.comments.store', ['book' => $book]) }}" method="POST">
            @csrf
            <div class="form-group">
                <input placeholder="your comment here." type="text" class="form-control" id="body" name="body"/>
            </div>

            <div class="form-group">
                <button type="submit" class="btn btn-primary btn-block">Comment</button>
            </div>
        </form>
    </div>
@endsection

```