Feuerwehr-eppingen/app/Models/Traits/Documents.php

80 lines
1.8 KiB
PHP

<?php
namespace App\Models\Traits;
trait Documents
{
protected $postType;
public function getModelName()
{
$class = (new \ReflectionClass($this))->getShortName();
$class = strtolower($class)."s";
return $class;
}
public function documents()
{
return $this->hasMany('App\Models\Document', 'model_id');
// ->where('model', $this->postType);
}
public function previewDocument()
{
return $this->hasOne('App\Models\Document', 'model_id')
->where('is_preview', '=', 1);
// ->where('model', '=', $this->type);
}
/**********/
/* Scopes */
/**********/
public function scopeWithImages($query, $type = null)
{
$this->postType = $type;
$query = $query->select(['posts.*', 'documents.filename'])
->join('documents', function($join)
{
$join->on('model_id', '=', $this->getModelName().'.id');
$join->where('model', '=', $this->postType);
$join->where('is_preview', '=', true);
$join->where('mime_type', '=', 'image/jpeg');
});
return $query;
}
public function scopeWithoutImages($query, $type = null)
{
$this->postType = $type;
$query = $query->leftjoin('documents', function($join){
$join->on('model_id', '=', $this->getModelName().'.id');
$join->where('model', '=', $this->postType);
})
->whereNull('model_id');
return $query;
}
public function scopeGetDocuments($query, $type = null)
{
$this->postType = $type;
$query = $query->select([$this->getModelName().'.*', 'documents.filename'])
->with('documents')
->leftjoin('documents', function($join)
{
$join->on('model_id', '=', $this->getModelName().'.id');
$join->where('is_preview', '=', true);
$join->where('mime_type', '=', 'image/jpeg');
if($this->postType)
{
$join->where('model', '=', $this->postType);
}
});
return $query;
}
}