Contact.php
3.84 KB
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
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* App\Models\Contact
*
* @property int $id
* @property int $account_id
* @property int|null $organization_id
* @property string $first_name
* @property string $last_name
* @property string|null $email
* @property string|null $phone
* @property string|null $address
* @property string|null $city
* @property string|null $region
* @property string|null $country
* @property string|null $postal_code
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property-read mixed $name
* @property-read \App\Models\Organization|null $organization
* @method static \Illuminate\Database\Eloquent\Builder|Contact filter(array $filters)
* @method static \Illuminate\Database\Eloquent\Builder|Contact newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Contact newQuery()
* @method static \Illuminate\Database\Query\Builder|Contact onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|Contact orderByName()
* @method static \Illuminate\Database\Eloquent\Builder|Contact query()
* @method static \Illuminate\Database\Eloquent\Builder|Contact whereAccountId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Contact whereAddress($value)
* @method static \Illuminate\Database\Eloquent\Builder|Contact whereCity($value)
* @method static \Illuminate\Database\Eloquent\Builder|Contact whereCountry($value)
* @method static \Illuminate\Database\Eloquent\Builder|Contact whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Contact whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Contact whereEmail($value)
* @method static \Illuminate\Database\Eloquent\Builder|Contact whereFirstName($value)
* @method static \Illuminate\Database\Eloquent\Builder|Contact whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Contact whereLastName($value)
* @method static \Illuminate\Database\Eloquent\Builder|Contact whereOrganizationId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Contact wherePhone($value)
* @method static \Illuminate\Database\Eloquent\Builder|Contact wherePostalCode($value)
* @method static \Illuminate\Database\Eloquent\Builder|Contact whereRegion($value)
* @method static \Illuminate\Database\Eloquent\Builder|Contact whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|Contact withTrashed()
* @method static \Illuminate\Database\Query\Builder|Contact withoutTrashed()
* @mixin \Eloquent
*/
class Contact extends Model
{
use SoftDeletes;
public function organization()
{
return $this->belongsTo(Organization::class);
}
public function getNameAttribute()
{
return $this->first_name.' '.$this->last_name;
}
public function scopeOrderByName($query)
{
$query->orderBy('last_name')->orderBy('first_name');
}
public function scopeFilter($query, array $filters)
{
$query->when($filters['search'] ?? null, function ($query, $search) {
$query->where(function ($query) use ($search) {
$query->where('first_name', 'like', '%'.$search.'%')
->orWhere('last_name', 'like', '%'.$search.'%')
->orWhere('email', 'like', '%'.$search.'%')
->orWhereHas('organization', function ($query) use ($search) {
$query->where('name', 'like', '%'.$search.'%');
});
});
})->when($filters['trashed'] ?? null, function ($query, $trashed) {
if ($trashed === 'with') {
$query->withTrashed();
} elseif ($trashed === 'only') {
$query->onlyTrashed();
}
});
}
}