File: /var/www/ipsremont-demo/app/Models/Address.php
<?php
namespace App\Models;
use App\Models\Service\Service;
use App\Traits\Displayed;
use App\Traits\My;
use App\Traits\Sortable;
use App\User;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;
/**
* @property int $id
* @property string $name
* @property string $country
* @property string $city
* @property string $address
* @property string $email
* @property string $phone
* @property int $main
* @property int $service_id
* @property int $user_id
* @property string $deleted_at
* @property string $created_at
* @property string $updated_at
*/
class Address extends BaseModel
{
use SoftDeletes, Sortable, Displayed, My;
protected $table = 'address';
protected $fillable = [
'name',
'country',
'city',
'address',
'email',
'phone',
'main',
];
protected static $labels = [
'name' => 'address.name',
'country' => 'address.country',
'city' => 'address.city',
'address' => 'address.address',
'email' => 'address.email',
'phone' => 'address.phone',
'main' => 'address.main',
'service_id' => 'address.service_id',
'user_id' => 'address.user_id',
];
public static function columns()
{
return [
"fields" => [
[
"displayName" => 'address.name',
"field" => "name",
"sort" => true,
"sortType" => 'asc',
],
[
"displayName" => 'address.country',
"field" => "country",
"sort" => true,
"sortType" => 'asc',
],
[
"displayName" => 'address.city',
"field" => "city",
"sort" => true,
"sortType" => 'asc',
],
[
"displayName" => 'address.address',
"field" => "address",
"sort" => true,
"sortType" => 'asc',
],
// [
// "displayName" => 'address.email',
// "field" => "email",
// "sort" => true,
// "sortType" => 'asc'
// ],
// [
// "displayName" => 'address.phone',
// "field" => "phone",
// "sort" => true,
// "sortType" => 'asc'
// ],
[
"displayName" => 'address.main',
"field" => "main",
"sort" => true,
"sortType" => 'asc',
"class" => 'text-center',
],
// [
// "displayName" => 'address.service_id',
// "field" => "service_id",
// "sort" => true,
// "sortType" => 'asc'
// ],
// [
// "displayName" => 'address.user_id',
// "field" => "user_id",
// "sort" => true,
// "sortType" => 'asc'
// ],
// [
// "displayName" => 'grid.createdAt',
// "field" => "created_at",
// "sort" => true,
// "sortType" => 'asc'
// ],
[
"displayName" => '',
"field" => 'action',
"sort" => false,
],
],
"sortDefault" => [
[
"field" => 'name',
"sort" => 'asc',
],
],
];
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getCountry()
{
return $this->country;
}
public function getCity()
{
return $this->city;
}
public function getAddress()
{
return $this->address;
}
public function getEmail()
{
return $this->email;
}
public function getPhone()
{
return $this->phone;
}
public function getMain()
{
return $this->main;
}
public function getService()
{
return $this->service_id;
}
public function getUser()
{
return $this->user_id;
}
public function service()
{
return $this->belongsTo(Service::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
/**
* Scope a query to only include My items
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeMy($query)
{
if (Auth::check() && Auth::user()->isService()) {
$query->where('service_id', Auth::user()->getCurrentServiceId());
}
}
public static function main_address()
{
return [
1 => 'address.is_main',
0 => 'address.is_not_main',
];
}
public function getFullAddressAttribute()
{
return ($this->city)
? ['text' => $this->city . ', ' . $this->address, 'id' => $this->id]
: ['text' => $this->address, 'id' => $this->id];
}
public function getFullSimpleAddressAttribute()
{
return ($this->city)
? $this->city . ', ' . $this->address
: $this->address;
}
}