113 lines
2.8 KiB
PHP
113 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Shop;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
use Spatie\MediaLibrary\HasMedia;
|
|
use Rinvex\Categories\Traits\Categorizable;
|
|
use Rinvex\Tags\Traits\Taggable;
|
|
use Kirschbaum\PowerJoins\PowerJoins;
|
|
use Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin;
|
|
use Wildside\Userstamps\Userstamps;
|
|
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
|
|
|
|
use App\Traits\Model\HasComments;
|
|
use App\Traits\Model\Imageable;
|
|
|
|
class Article extends Model implements HasMedia
|
|
{
|
|
use Categorizable, EloquentJoin, HasComments, HasRelationships, Imageable, Powerjoins, Taggable, SoftDeletes, UserStamps;
|
|
|
|
protected $guarded = ['id'];
|
|
protected $table = 'shop_articles';
|
|
|
|
public function article_nature()
|
|
{
|
|
return $this->belongsTo(ArticleNature::class);
|
|
}
|
|
|
|
public function invoiceItems()
|
|
{
|
|
return $this->hasMany(InvoiceItem::class);
|
|
}
|
|
|
|
public function offers()
|
|
{
|
|
return $this->hasMany(Offer::class);
|
|
}
|
|
|
|
/*
|
|
public function prices()
|
|
{
|
|
return $this->hasManyDeep(PriceListValue::class, [Offer::class, Tariff::class, PriceList::class]);
|
|
}
|
|
*/
|
|
|
|
public function product()
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
public function tags()
|
|
{
|
|
return $this->morphToMany(Tag::class, 'taggable');
|
|
}
|
|
|
|
public function tariffs()
|
|
{
|
|
return $this->hasManyThrough(Tariff::class, Offer::class, 'article_id', 'id', 'id', 'tariff_id');
|
|
// return $this->belongsToMany(Tariff::class, Offer::$table, 'id', 'id', 'tariff_id', 'tariff_id');
|
|
}
|
|
|
|
public function scopeByArticle($query, $id)
|
|
{
|
|
return $query->where($this->table . '.id', $id);
|
|
}
|
|
|
|
public function scopeByAutocomplete($query, $str)
|
|
{
|
|
return $query->where($this->table . '.name', 'LIKE', "%${str}%");
|
|
}
|
|
|
|
public function scopeByArticleNature($query, $id)
|
|
{
|
|
return $query->where($this->table . '.article_nature_id', $id);
|
|
}
|
|
|
|
public function scopeByCategory($query, $category_id)
|
|
{
|
|
return $query->whereHas('categories', function ($query) use ($category_id) {
|
|
$query->where('id', $category_id);
|
|
});
|
|
}
|
|
|
|
public function scopeByProduct($query, $model)
|
|
{
|
|
return $query->where($this->table . '.product_type', $model);
|
|
}
|
|
|
|
public function scopeByProductId($query, $model_id)
|
|
{
|
|
return $query->where($this->table . '.product_id', $model_id);
|
|
}
|
|
|
|
public function scopeWithOffers($query)
|
|
{
|
|
return $query->has('offers');
|
|
}
|
|
|
|
public function scopeWithAvailableOffers($query)
|
|
{
|
|
return $query->whereHas('offers', function ($query) {
|
|
$query->where('status_id', 1);
|
|
});
|
|
}
|
|
|
|
public function scopeVisible($query)
|
|
{
|
|
return $query->where($this->table . '.visible', 1);
|
|
}
|
|
}
|