sarga/packages/Sarga/Brand/src/Models/Brand.php

98 lines
2.1 KiB
PHP
Raw Normal View History

2022-01-25 06:54:15 +00:00
<?php namespace Sarga\Brand\Models;
use Illuminate\Database\Eloquent\Model;
2022-01-26 14:14:22 +00:00
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
2022-01-27 15:07:05 +00:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2022-01-25 06:54:15 +00:00
use Illuminate\Support\Facades\Storage;
2022-04-16 15:02:29 +00:00
use Laravel\Scout\Searchable;
2022-01-25 06:54:15 +00:00
use Sarga\Brand\Contracts\Brand as BrandContract;
2022-01-26 14:14:22 +00:00
use Webkul\Category\Models\CategoryProxy;
use Webkul\Marketplace\Models\SellerProxy;
2022-12-17 08:08:28 +00:00
use Webkul\Product\Models\ProductFlatProxy;
2022-01-26 14:14:22 +00:00
use Webkul\Product\Models\ProductProxy;
2022-01-25 11:16:22 +00:00
2022-01-25 06:54:15 +00:00
class Brand extends Model implements BrandContract
{
2022-04-16 15:02:29 +00:00
use Searchable;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'brands';
2022-01-25 06:54:15 +00:00
protected $fillable = [
'position',
'status',
'code',
'name'
];
2022-04-16 15:02:29 +00:00
/**
* Get the index name for the model.
*
* @return string
*/
public function searchableAs()
{
return 'brands_index';
}
2022-04-17 09:32:04 +00:00
public function toSearchableArray()
{
return [
'id' => $this->id,
'name' => $this->name,
2023-09-15 09:42:39 +00:00
'type' => 'brand',
'position' => $this->position,
2022-04-17 09:32:04 +00:00
];
}
2022-04-17 09:41:06 +00:00
public function shouldBeSearchable()
{
2023-09-15 09:42:39 +00:00
return $this->status;
2022-04-17 09:41:06 +00:00
}
2022-01-25 06:54:15 +00:00
/**
* Get image url for the category image.
*/
public function image_url()
{
if (! $this->image) {
return;
}
return Storage::url($this->image);
}
/**
* Get image url for the category image.
*/
public function getImageUrlAttribute()
{
return $this->image_url();
}
2022-01-26 14:14:22 +00:00
/**
2022-04-21 12:42:42 +00:00
* The products that belong to the brand.
2022-01-26 14:14:22 +00:00
*/
2022-01-27 15:07:05 +00:00
public function products(): HasMany
2022-01-26 14:14:22 +00:00
{
2022-01-27 15:07:05 +00:00
return $this->hasMany(ProductProxy::modelClass(), 'brand_id');
2022-01-26 14:14:22 +00:00
}
2022-12-17 08:31:48 +00:00
public function flats():HasMany{
return $this->hasMany(ProductFlatProxy::modelClass(), 'brand_id');
2022-12-17 08:08:28 +00:00
}
2022-01-26 14:14:22 +00:00
public function sellers():BelongsToMany
{
return $this->belongsToMany(SellerProxy::modelClass(), 'seller_brands');
}
public function categories():BelongsToMany
{
return $this->belongsToMany(CategoryProxy::modelClass(), 'category_brands');
}
2022-01-25 06:54:15 +00:00
}