first upload all files
This commit is contained in:
54
Modules/Support/Eloquent/Model.php
Normal file
54
Modules/Support/Eloquent/Model.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Support\Eloquent;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Database\Eloquent\Model as Eloquent;
|
||||
|
||||
abstract class Model extends Eloquent
|
||||
{
|
||||
/**
|
||||
* Perform any actions required before the model boots.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected static function booting()
|
||||
{
|
||||
static::saved(function ($entity) {
|
||||
$entity->clearEntityTaggedCache();
|
||||
});
|
||||
|
||||
static::deleted(function ($entity) {
|
||||
$entity->clearEntityTaggedCache();
|
||||
});
|
||||
}
|
||||
|
||||
public static function queryWithoutEagerRelations()
|
||||
{
|
||||
return (new static)->newQueryWithoutEagerRelations();
|
||||
}
|
||||
|
||||
public function newQueryWithoutEagerRelations()
|
||||
{
|
||||
return $this->registerGlobalScopes(
|
||||
$this->newModelQuery()->withCount($this->withCount)
|
||||
);
|
||||
}
|
||||
|
||||
public function clearEntityTaggedCache()
|
||||
{
|
||||
Cache::tags($this->getTable())->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new active global scope on the model.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function addActiveGlobalScope()
|
||||
{
|
||||
static::addGlobalScope('active', function ($query) {
|
||||
$query->where('is_active', true);
|
||||
});
|
||||
}
|
||||
}
|
||||
58
Modules/Support/Eloquent/Sluggable.php
Normal file
58
Modules/Support/Eloquent/Sluggable.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Support\Eloquent;
|
||||
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
trait Sluggable
|
||||
{
|
||||
/**
|
||||
* Boot the trait.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function bootSluggable()
|
||||
{
|
||||
static::creating(function ($entity) {
|
||||
$entity->setSlug();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the slug attribute.
|
||||
*
|
||||
* @param string $value
|
||||
* @return void
|
||||
*/
|
||||
public function setSlug($value = null)
|
||||
{
|
||||
if (is_null($value)) {
|
||||
$value = $this->getAttribute($this->slugAttribute);
|
||||
}
|
||||
|
||||
$this->attributes['slug'] = $this->generateSlug($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate slug by the given value.
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
private function generateSlug($value)
|
||||
{
|
||||
$slug = str_slug($value) ?: slugify($value);
|
||||
|
||||
$query = $this->where('slug', $slug)->withoutGlobalScope('active');
|
||||
|
||||
if (array_has(class_uses($this), SoftDeletes::class)) {
|
||||
$query->withTrashed();
|
||||
}
|
||||
|
||||
if ($query->exists()) {
|
||||
$slug .= '-' . str_random(8);
|
||||
}
|
||||
|
||||
return $slug;
|
||||
}
|
||||
}
|
||||
44
Modules/Support/Eloquent/Translatable.php
Normal file
44
Modules/Support/Eloquent/Translatable.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Support\Eloquent;
|
||||
|
||||
use Astrotomic\Translatable\Translatable as AstrotomicTranslatable;
|
||||
|
||||
trait Translatable
|
||||
{
|
||||
use AstrotomicTranslatable;
|
||||
|
||||
/**
|
||||
* Save the model to the database.
|
||||
*
|
||||
* @param array $options
|
||||
* @return bool
|
||||
*/
|
||||
public function save(array $options = [])
|
||||
{
|
||||
if (parent::save($options)) {
|
||||
return $this->saveTranslations();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This scope filters results by checking the translation fields.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $key
|
||||
* @param array $values
|
||||
* @param string $locale
|
||||
* @return \Illuminate\Database\Eloquent\Builder|static
|
||||
*/
|
||||
public function scopeWhereTranslationIn($query, $key, array $values, $locale = null)
|
||||
{
|
||||
return $query->whereHas('translations', function ($query) use ($key, $values, $locale) {
|
||||
$query->whereIn($key, $values)
|
||||
->when(! is_null($locale), function ($query) use ($locale) {
|
||||
$query->where('locale', $locale);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
27
Modules/Support/Eloquent/TranslationModel.php
Normal file
27
Modules/Support/Eloquent/TranslationModel.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Support\Eloquent;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
abstract class TranslationModel extends Model
|
||||
{
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* Perform any actions required before the model boots.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function booting()
|
||||
{
|
||||
static::addGlobalScope('locale', function ($query) {
|
||||
$query->whereIn('locale', [locale(), config('app.fallback_locale')]);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user