forked from eatvio/eatvio-infra
110 lines
3.1 KiB
PHP
110 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
/**
|
|
* App\Models\DietComment
|
|
*
|
|
* @property int $id
|
|
* @property int $user_id
|
|
* @property int $diet_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\Diet|null $diet
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\DietCommentUpvote> $upvotes
|
|
* @property-read int|null $upvotes_count
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder|DietComment newModelQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|DietComment newQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|DietComment query()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|DietComment whereBody($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|DietComment whereCreatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|DietComment whereDietId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|DietComment whereGrade($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|DietComment whereId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|DietComment whereUpdatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|DietComment whereUserId($value)
|
|
*
|
|
* @mixin \Eloquent
|
|
*/
|
|
class DietComment extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
public function author(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
public function diet(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Diet::class, 'diet_id');
|
|
}
|
|
|
|
public function upvotes(): HasMany
|
|
{
|
|
return $this->hasMany(DietCommentUpvote::class, 'comment_id');
|
|
}
|
|
|
|
public function isUpVoted(): bool|int
|
|
{
|
|
if (auth()->user()) {
|
|
return auth()
|
|
->user()
|
|
->diet_comment_upvotes()
|
|
->forDietCommentsUpVote($this)
|
|
->count();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function isDownVoted(): bool|int
|
|
{
|
|
if (auth()->user()) {
|
|
return auth()
|
|
->user()
|
|
->diet_comment_upvotes()
|
|
->forDietCommentsDownVote($this)
|
|
->count();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function removeUpVote(): bool|int
|
|
{
|
|
if (auth()->user()) {
|
|
return auth()
|
|
->user()
|
|
->diet_comment_upvotes()
|
|
->forDietCommentsUpVote($this)
|
|
->delete();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function removeDownVote(): bool
|
|
{
|
|
if (auth()->user()) {
|
|
return auth()
|
|
->user()
|
|
->diet_comment_upvotes()
|
|
->forDietCommentsDownVote($this)
|
|
->delete();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|