File: D:/HostingSpaces/netwerkbrabant/netwerkbrabant.nl/app/KommaApp/Companies/Models/Company.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\KommaApp\Companies\Models;
use App\KommaApp\CompanyCategories\Models\CompanyCategory;
use App\KommaApp\Kms\Core\Entities\DisplayNameTrait;
use App\KommaApp\Documents\DocumentsTrait;
use App\KommaApp\Documents\Kms\DocumentableInterface;
use App\KommaApp\Documents\Models\Document;
use App\KommaApp\Images\Models\Image;
use App\KommaApp\Kms\Core\AbstractTranslatableModel;
use App\KommaApp\Kms\Core\HasImagesInterface;
use App\KommaApp\Languages\Models\Language;
use App\KommaApp\MagazineArticles\Models\MagazineArticle;
use App\KommaApp\Regions\Models\Region;
use App\KommaApp\Users\Models\User;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Laravel\Scout\Searchable;
/**
* Class Company
*
* @package App\KommaApp\Companies\Models
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Languages\Models\Language[] $languages
* @property-read \App\KommaApp\Sites\Models\Site $site
* @property-read \App\KommaApp\Companies\Models\CompanyTranslation $translation
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Companies\Models\CompanyTranslation[] $translations
* @mixin \Eloquent
* @property int $id
* @property int $site_id
* @property int|null $region_id
* @property int $active
* @property string $name
* @property string $slug
* @property string $address
* @property string $postal
* @property string $city
* @property string $email
* @property string $phone
* @property string $site_url
* @property boolean $invoice_default_to_company
* @property string $invoice_address
* @property string $invoice_postal
* @property string $invoice_city
* @property string $invoice_email
* @property string $invoice_phone
* @property string $instagram
* @property string $linkedin
* @property string $facebook
* @property string $twitter
* @property string $youtube
* @property \Carbon\Carbon|null $created_at
* @property \Carbon\Carbon|null $updated_at
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Companies\Models\Company whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Companies\Models\Company whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Companies\Models\Company whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Companies\Models\Company whereSiteId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Companies\Models\Company whereUpdatedAt($value)
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Documents\Models\Document[] $documents
*/
class Company extends AbstractTranslatableModel implements DocumentableInterface
{
const LATEST_ARTICLES = 2;
use DocumentsTrait;
use DisplayNameTrait;
use Searchable;
protected $table = 'companies';
protected $class = Company::class;
/*
* Transient properties on Eloquent models
* These are not saved to database.
*/
public $thumbnail = false;
// public $parent_id;
protected $fillable = ['active', 'name', 'region_id','company_category_id', 'slug', 'address', 'postal', 'city', 'email', 'phone', 'site_url', 'invoice_default_to_company','invoice_address', 'invoice_postal', 'invoice_city', 'invoice_email', 'invoice_phone', 'instagram', 'linkedin', 'facebook', 'twitter', 'youtube' ];
public function toSearchableArray()
{
return $this->only('id', 'name', 'address', 'city');
}
public function languages(): BelongsToMany
{
return $this->belongsToMany(Language::class, 'company_translations')
->withPivot('description')
->withTimestamps();
}
/**
* Gets the translation models for this model
*
* @return HasMany that resolves to AbstractTranslationModel instances
*/
public function translations(): HasMany
{
return $this->hasMany(CompanyTranslation::class);
}
/**
* Get the company category
*
* @return BelongsTo
*/
public function companyCategory(): BelongsTo
{
return $this->belongsTo(CompanyCategory::class);
}
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class, 'company_users');
}
public function region(): BelongsTo
{
return $this->belongsTo(Region::class);
}
public function articles(): HasMany
{
return $this->hasMany(MagazineArticle::class)
->has('magazines')
->orderBy('id', 'desc');
}
public function latestArticles(): HasMany
{
return $this->hasMany(MagazineArticle::class)
->has('magazines')
->orderBy('id', 'desc')
->take(self::LATEST_ARTICLES);
}
public function setCompanyCategoryIdAttribute($value){
if($value === '')
$this->attributes['company_category_id'] = null;
else
$this->attributes['company_category_id'] = $value;
}
public function setRegionIdAttribute($value){
if($value === '')
$this->attributes['region_id'] = null;
else
$this->attributes['region_id'] = $value;
}
/**
* Get the logo images through documents belonging to this model
*
* @return \Illuminate\Database\Eloquent\Relations\hasMany
*/
public function images():MorphMany
{
return $this->morphMany(Document::class, 'documentable')
->where('mime_type', 'LIKE', 'image/%')
->where('key', 'logo');
}
/**
* Get the logo images through documents belonging to this model
*
* @return \Illuminate\Database\Eloquent\Relations\hasMany
*/
public function logo():MorphMany
{
return $this->morphMany(Document::class, 'documentable')
->where('mime_type', 'LIKE', 'image/%')
->where('key', 'logo');
}
/**
* Get the impression images through documents belonging to this model
*
* @return \Illuminate\Database\Eloquent\Relations\hasMany
*/
public function impression():MorphMany
{
return $this->morphMany(Document::class, 'documentable')
->where('mime_type', 'LIKE', 'image/%')
->where('key', 'impression');
}
}