forked from eatvio/eatvio-infra
eating plan added
This commit is contained in:
57
app/Models/Plan.php
Normal file
57
app/Models/Plan.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
/* use Illuminate\Database\Eloquent\Casts\Attribute; */
|
||||
use App\Models\PlanLike;
|
||||
|
||||
/* use Illuminate\Database\Eloquent\Casts\AsArrayObject; */
|
||||
|
||||
/* use Tonysm\RichTextLaravel\Models\Traits\HasRichText; */
|
||||
/* use Tonysm\RichTextLaravel\Casts\AsRichTextContent; */
|
||||
|
||||
class Plan extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $withCount = [
|
||||
'likes',
|
||||
'comments'
|
||||
];
|
||||
|
||||
public function author()
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'plan_author', 'plan_id', 'user_id');
|
||||
}
|
||||
|
||||
public function comments()
|
||||
{
|
||||
return $this->hasMany(PlanComment::class);
|
||||
}
|
||||
|
||||
public function likes(): HasMany
|
||||
{
|
||||
return $this->hasMany(PlanLike::class);
|
||||
}
|
||||
|
||||
public function isLiked(): bool
|
||||
{
|
||||
if (auth()->user()) {
|
||||
return auth()->user()->plan_likes()->forPlan($this)->count();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function removeLike(): bool
|
||||
{
|
||||
if (auth()->user()) {
|
||||
return auth()->user()->plan_likes()->forPlan($this)->delete();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user