forked from eatvio/eatvio-infra
111 lines
3.3 KiB
PHP
111 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
/**
|
|
* App\Models\RecipeComment
|
|
*
|
|
* @property int $id
|
|
* @property int $user_id
|
|
* @property int $recipe_id
|
|
* @property int $grade
|
|
* @property string|null $body
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
*
|
|
* @property-read \App\Models\User $author
|
|
* @property-read \App\Models\Recipe|null $recipe
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\RecipeCommentUpvote> $upvotes
|
|
* @property-read int|null $upvotes_count
|
|
*
|
|
* @method static \Database\Factories\RecipeCommentFactory factory($count = null, $state = [])
|
|
* @method static \Illuminate\Database\Eloquent\Builder|RecipeComment newModelQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|RecipeComment newQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|RecipeComment query()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|RecipeComment whereBody($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|RecipeComment whereCreatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|RecipeComment whereGrade($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|RecipeComment whereId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|RecipeComment whereRecipeId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|RecipeComment whereUpdatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|RecipeComment whereUserId($value)
|
|
*
|
|
* @mixin \Eloquent
|
|
*/
|
|
class RecipeComment extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
public function author(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
public function recipe(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Recipe::class, 'recipe_id');
|
|
}
|
|
|
|
public function upvotes(): HasMany
|
|
{
|
|
return $this->hasMany(RecipeCommentUpvote::class, 'recipe_comment_id');
|
|
}
|
|
|
|
public function isUpVoted(): bool|int
|
|
{
|
|
$curUser = auth()->user();
|
|
if ($curUser) {
|
|
return $curUser
|
|
->recipe_comment_upvotes()
|
|
->forRecipeCommentsUpVote($this)
|
|
->count();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function isDownVoted(): bool|int
|
|
{
|
|
$curUser = auth()->user();
|
|
if ($curUser) {
|
|
return $curUser
|
|
->recipe_comment_upvotes()
|
|
->forRecipeCommentsDownVote($this)
|
|
->count();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function removeUpVote(): bool|int
|
|
{
|
|
$curUser = auth()->user();
|
|
if ($curUser) {
|
|
return $curUser
|
|
->recipe_comment_upvotes()
|
|
->forRecipeCommentsUpVote($this)
|
|
->delete();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function removeDownVote(): bool|int
|
|
{
|
|
$curUser = auth()->user();
|
|
if ($curUser) {
|
|
return $curUser
|
|
->recipe_comment_upvotes()
|
|
->forRecipeCommentsDownVote($this)
|
|
->delete();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|