pingcrm/app/Models/Contact.php

54 lines
1.6 KiB
PHP
Raw Normal View History

2019-03-18 08:53:00 -03:00
<?php
2020-09-08 19:45:49 -03:00
namespace App\Models;
2019-03-18 08:53:00 -03:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2019-03-18 08:53:00 -03:00
use Illuminate\Database\Eloquent\SoftDeletes;
class Contact extends Model
{
use HasFactory;
2019-03-18 08:53:00 -03:00
use SoftDeletes;
public function resolveRouteBinding($value, $field = null)
{
return $this->where($field ?? 'id', $value)->withTrashed()->firstOrFail();
}
2019-03-18 08:53:00 -03:00
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.'%')
2019-03-18 08:53:00 -03:00
->orWhereHas('organization', function ($query) use ($search) {
$query->where('name', 'like', '%'.$search.'%');
2019-03-18 08:53:00 -03:00
});
});
})->when($filters['trashed'] ?? null, function ($query, $trashed) {
if ($trashed === 'with') {
$query->withTrashed();
} elseif ($trashed === 'only') {
$query->onlyTrashed();
}
});
}
}