Add method to get offers by articles with siblings, enhance display
This commit is contained in:
@@ -13,9 +13,9 @@ class ArticleController extends Controller
|
||||
public function show($id)
|
||||
{
|
||||
$data = self::init();
|
||||
$data['article'] = Articles::getArticle($id);
|
||||
// dump($data);
|
||||
// exit;
|
||||
$data['article'] = Articles::getArticleToSell($id);
|
||||
dump($data);
|
||||
exit;
|
||||
return view('Shop.Articles.show', $data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,6 +50,11 @@ class Article extends Model implements HasMedia
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
public function siblings()
|
||||
{
|
||||
return $this->hasMany(Article::class, 'name', 'name');
|
||||
}
|
||||
|
||||
public function tags()
|
||||
{
|
||||
return $this->morphToMany(Tag::class, 'taggable');
|
||||
|
||||
@@ -4,11 +4,12 @@ namespace App\Models\Shop;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
use Znck\Eloquent\Traits\BelongsToThrough;
|
||||
use App\Traits\Model\HasComments;
|
||||
|
||||
class Offer extends Model
|
||||
{
|
||||
use HasComments;
|
||||
use BelongsToThrough, HasComments;
|
||||
|
||||
protected $guarded = ['id'];
|
||||
protected $table = 'shop_offers';
|
||||
@@ -18,6 +19,14 @@ class Offer extends Model
|
||||
return $this->belongsTo(Article::class);
|
||||
}
|
||||
|
||||
public function article_nature()
|
||||
{
|
||||
return $this->belongsToThrough(ArticleNature::class, Article::class, null, '', [
|
||||
'App\Models\Shop\Article' => 'article_id',
|
||||
'App\Models\Shop\ArticleNature' => 'article_nature_id',
|
||||
]);
|
||||
}
|
||||
|
||||
public function tariff()
|
||||
{
|
||||
return $this->belongsTo(Tariff::class);
|
||||
@@ -63,6 +72,11 @@ class Offer extends Model
|
||||
return $query->where($this->table . '.article_id', $id);
|
||||
}
|
||||
|
||||
public function scopeByArticles($query, $ids)
|
||||
{
|
||||
return $query->whereIn($this->table . '.article_id', $ids);
|
||||
}
|
||||
|
||||
public function scopeByOffer($query, $id)
|
||||
{
|
||||
return $query->where($this->table . '.id', $id);
|
||||
|
||||
@@ -27,6 +27,35 @@ class Articles
|
||||
return $export;
|
||||
}
|
||||
|
||||
public static function getOffersGroupedByNature($id)
|
||||
{
|
||||
$article_ids = self::getSiblingsIds($id);
|
||||
$offers = Offers::getOffersByArticles($article_ids);
|
||||
dump($offers->toArray());
|
||||
foreach ($offers as $offer) {
|
||||
$data[$offer->article_nature->name][] = [
|
||||
'name' => $offer->variation->name,
|
||||
'tariff' => $offer->tariff,
|
||||
];
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function getSiblingsIDs($id)
|
||||
{
|
||||
return self::getSiblings($id)->pluck('id')->toArray();
|
||||
}
|
||||
|
||||
public static function getSiblings($id)
|
||||
{
|
||||
return Article::with('siblings')->find($id)->siblings;
|
||||
}
|
||||
|
||||
public static function getOffersById($id)
|
||||
{
|
||||
return Offers::getOffersByArticle($id);
|
||||
}
|
||||
|
||||
public static function getOptions()
|
||||
{
|
||||
return Article::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
||||
@@ -47,6 +76,20 @@ class Articles
|
||||
return Article::orderBy('name', 'asc')->get();
|
||||
}
|
||||
|
||||
public static function getArticleToSell($id)
|
||||
{
|
||||
$article = self::get($id);
|
||||
$data = $article->toArray();
|
||||
$data['description'] = (!empty($article->description)) ? $article->description : $article->product->description;
|
||||
$data['image'] = self::getPreview($article->image);
|
||||
$data['image_big'] = self::getImage($article->image);
|
||||
$data['inherited'] = self::getInherited($id);
|
||||
$data['categories'] = self::getCategoriesNameByArticle($article);
|
||||
$data['tags'] = self::getTagsSlugByArticle($article);
|
||||
$data['offers'] = self::getOffersById($id)->toArray();
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function getArticle($id)
|
||||
{
|
||||
$article = self::get($id);
|
||||
|
||||
@@ -7,6 +7,16 @@ use App\Models\Shop\Offer;
|
||||
class Offers
|
||||
{
|
||||
|
||||
public static function getOffersByArticles($articles_id)
|
||||
{
|
||||
return Offer::active()->with(['article_nature', 'tariff.price_lists.price_list_values', 'variation'])->byArticles($articles_id)->get();
|
||||
}
|
||||
|
||||
public static function getOffersByArticle($article_id)
|
||||
{
|
||||
return Offer::active()->with('variation')->byArticle($article_id)->get();
|
||||
}
|
||||
|
||||
public static function getThumbSrcById($id)
|
||||
{
|
||||
return self::getThumbSrc(self::get($id));
|
||||
|
||||
Reference in New Issue
Block a user