Spot::getImageAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Spot extends Model
8
{
9
    protected $fillable = [
10
        'name',
11
        'slug',
12
        'address',
13
        'email',
14
        'phone_number',
15
        'city',
16
        'country_id',
17
        'image',
18
        'website',
19
        'latitude',
20
        'longitude',
21
        'is_approved',
22
        'is_featured',
23
    ];
24
25
    public function getRouteKeyName()
26
    {
27
        return 'slug';
28
    }
29
30
    public function country()
31
    {
32
        return $this->belongsTo(Country::class);
33
    }
34
35
    public function getImageAttribute($imagePath)
36
    {
37
        return ($imagePath ? asset("storage/$imagePath") : null);
38
    }
39
40
    /**
41
     * Returns the 3 most recent Spots that have an image
42
     * and are approved
43
     *
44
     * @param $query
45
     * @param int $take
46
     */
47
    public function scopeMostRecent($query, $take = 3)
48
    {
49
        $query->with('country')
50
            ->where('is_approved', true)
51
            ->where('image', '!=', null)
52
            ->orderBy('created_at', 'desc')
53
            ->take($take);
54
    }
55
56
    public function scopeFeatured($query, $take = 3)
57
    {
58
        $query->with('country')
59
            ->where('is_approved', true)
60
            ->orderBy('is_featured', 'desc')
61
            ->orderBy('created_at', 'desc')
62
            ->take($take);
63
    }
64
}
65