first commit
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
class Ingredient extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'category',
|
||||
];
|
||||
|
||||
public function recipes(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Recipe::class, 'recipe_ingredient')
|
||||
->withPivot('quantity', 'unit', 'notes', 'sort_order')
|
||||
->withTimestamps();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
|
||||
class Recipe extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'slug',
|
||||
'description',
|
||||
'prep_time',
|
||||
'cook_time',
|
||||
'total_time',
|
||||
'servings',
|
||||
'difficulty',
|
||||
'cuisine',
|
||||
'category',
|
||||
'calories',
|
||||
'featured_image',
|
||||
'video_url',
|
||||
'status',
|
||||
'views_count',
|
||||
'rating_avg',
|
||||
'rating_count',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'prep_time' => 'integer',
|
||||
'cook_time' => 'integer',
|
||||
'total_time' => 'integer',
|
||||
'servings' => 'integer',
|
||||
'calories' => 'integer',
|
||||
'status' => 'integer',
|
||||
'views_count' => 'integer',
|
||||
'rating_avg' => 'decimal:2',
|
||||
'rating_count' => 'integer',
|
||||
];
|
||||
|
||||
public function ingredients(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Ingredient::class, 'recipe_ingredient')
|
||||
->withPivot('quantity', 'unit', 'notes', 'sort_order')
|
||||
->withTimestamps()
|
||||
->orderBy('recipe_ingredient.sort_order');
|
||||
}
|
||||
|
||||
public function steps(): HasMany
|
||||
{
|
||||
return $this->hasMany(RecipeStep::class)->orderBy('sort_order');
|
||||
}
|
||||
|
||||
public function images(): HasMany
|
||||
{
|
||||
return $this->hasMany(RecipeImage::class)->orderBy('sort_order');
|
||||
}
|
||||
|
||||
public function nutrition(): HasOne
|
||||
{
|
||||
return $this->hasOne(RecipeNutrition::class);
|
||||
}
|
||||
|
||||
public function tags(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Tag::class, 'recipe_tag')->withTimestamps();
|
||||
}
|
||||
|
||||
public function ratings(): HasMany
|
||||
{
|
||||
return $this->hasMany(RecipeRating::class);
|
||||
}
|
||||
|
||||
public function comments(): HasMany
|
||||
{
|
||||
return $this->hasMany(RecipeComment::class)->latest();
|
||||
}
|
||||
|
||||
public function favoritedBy(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'user_favorites')->withTimestamps();
|
||||
}
|
||||
|
||||
public function isFavoritedBy(?User $user): bool
|
||||
{
|
||||
if (!$user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->favoritedBy()->where('user_id', $user->id)->exists();
|
||||
}
|
||||
|
||||
public function userRating(?User $user): ?int
|
||||
{
|
||||
if (!$user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->ratings()->where('user_id', $user->id)->value('rating');
|
||||
}
|
||||
|
||||
public function getRecipeImage()
|
||||
{
|
||||
if ($this->featured_image) {
|
||||
return $this->featured_image;
|
||||
}
|
||||
|
||||
if ($this->images()) {
|
||||
return $this->images()->first()->image_path;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class RecipeComment extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'recipe_id',
|
||||
'comment',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function recipe(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Recipe::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class RecipeImage extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'recipe_id',
|
||||
'image_path',
|
||||
'title',
|
||||
'description',
|
||||
'is_featured',
|
||||
'sort_order',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'recipe_id' => 'integer',
|
||||
'is_featured' => 'boolean',
|
||||
'sort_order' => 'integer',
|
||||
];
|
||||
|
||||
public function recipe(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Recipe::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class RecipeNutrition extends Model
|
||||
{
|
||||
protected $table = 'recipe_nutrition';
|
||||
|
||||
protected $fillable = [
|
||||
'recipe_id',
|
||||
'calories',
|
||||
'protein',
|
||||
'carbohydrates',
|
||||
'fat',
|
||||
'saturated_fat',
|
||||
'fiber',
|
||||
'sugar',
|
||||
'sodium',
|
||||
'cholesterol',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'recipe_id' => 'integer',
|
||||
'calories' => 'integer',
|
||||
'protein' => 'decimal:2',
|
||||
'carbohydrates' => 'decimal:2',
|
||||
'fat' => 'decimal:2',
|
||||
'saturated_fat' => 'decimal:2',
|
||||
'fiber' => 'decimal:2',
|
||||
'sugar' => 'decimal:2',
|
||||
'sodium' => 'decimal:2',
|
||||
'cholesterol' => 'decimal:2',
|
||||
];
|
||||
|
||||
public function recipe(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Recipe::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class RecipeRating extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'recipe_id',
|
||||
'rating',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'rating' => 'integer',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function recipe(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Recipe::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class RecipeStep extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'recipe_id',
|
||||
'step_number',
|
||||
'title',
|
||||
'instruction',
|
||||
'image',
|
||||
'time_estimate',
|
||||
'sort_order',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'recipe_id' => 'integer',
|
||||
'step_number' => 'integer',
|
||||
'time_estimate' => 'integer',
|
||||
'sort_order' => 'integer',
|
||||
];
|
||||
|
||||
public function recipe(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Recipe::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
class Tag extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
];
|
||||
|
||||
public function recipes(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Recipe::class, 'recipe_tag')->withTimestamps();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
|
||||
public function favoriteRecipes()
|
||||
{
|
||||
return $this->belongsToMany(Recipe::class, 'user_favorites')->withTimestamps();
|
||||
}
|
||||
|
||||
public function ratings()
|
||||
{
|
||||
return $this->hasMany(RecipeRating::class);
|
||||
}
|
||||
|
||||
public function comments()
|
||||
{
|
||||
return $this->hasMany(RecipeComment::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user