File size: 3,351 Bytes
6966756
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php

namespace App\Models;

use App\Constants\Status;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;


class Prompt extends Model
{

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

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function orderDetails()
    {
        return $this->hasMany(OrderDetails::class, 'prompt_id');
    }

    public function toolVersion()
    {
        return $this->belongsTo(ToolVersion::class);
    }

    public function tool()
    {
        return $this->belongsTo(Tool::class);
    }

    public function promptImages()
    {
        return $this->hasMany(PromptImage::class);
    }

    public function carts()
    {
        return $this->hasMany(Cart::class, 'prompt_id');
    }

    public function reviews()
    {
        return $this->hasMany(Review::class);
    }

    public function favorites()
    {
        return $this->belongsToMany(User::class, 'favorites', 'prompt_id', 'user_id');
    }

    public function promptStatusBadge(): Attribute
    {
        return new Attribute(function () {
            $html = '';
            if ($this->status == Status::PROMPT_APPROVED) {
                $html = '<span class="badge custom--badge badge--success">' . trans("Approved") . '</span>';
            } elseif ($this->status == Status::PROMPT_PENDING) {
                $html = '<span class="badge custom--badge badge--warning">' . trans("Pending") . '</span>';
            } elseif ($this->status == Status::PROMPT_REJECTED) {
                $html = '<span class="badge custom--badge badge--danger">' . trans("Rejected") . '</span>';
            }
            return $html;
        });
    }


    public function getSalesCountAttribute(): int
    {
        return $this->orderDetails()
            ->whereHas('order', function ($query) {
                $query->where('status', Status::ORDER_COMPLETED);
            })
            ->count();
    }

    //Scope
    public function scopePending($query)
    {
        return $query->where('status', Status::PROMPT_PENDING);
    }
    public function scopeApproved($query)
    {
        return $query->where('status', Status::PROMPT_APPROVED);
    }
    public function scopeRejected($query)
    {
        return $query->where('status', Status::PROMPT_REJECTED);
    }

    public function scopeTrending($query)
    {
        return $query->withCount(['orderDetails' => function($q){
            $q->whereHas('order', function($order){
                $order->where('status', Status::ORDER_COMPLETED);
            })
            ->whereDate('created_at', '>=' ,now()->subDays(15));
        }])
        ->orderBy('order_details_count', 'DESC');
    }

    public function scopeFeatured($query)
    {
        return $query->where('is_featured', Status::PROMPT_FEATURED);
    }

    public function scopeSimilarPrompts($query, $prompt) {
        return $query->approved()
        ->where('id', '!=', $prompt->id)
        ->where(function ($query) use ($prompt) {
            $query->where('category_id', $prompt->category_id)
                ->orWhere('tool_id', $prompt->tool_id);
        })
        ->inRandomOrder();
    }

    public function getAvgRating() {
        return $this->reviews()->avg('rating') ?? 0;
    }
}