invert query from offers->articles to articles->offers

This commit is contained in:
Ludovic CANDELLIER
2022-01-05 22:05:30 +01:00
parent 8a1573d425
commit b2f5cc4a45
17 changed files with 121 additions and 93 deletions

View File

@@ -20,6 +20,8 @@ class HomeController extends Controller
{
$data = self::init();
$data['offers'] = Offers::getLast()->toArray();
dump($data);
exit;
return view('Shop.home', $data);
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace App\Http\Controllers\Shop;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\Shop\Offers;
class OfferController extends Controller
{
public function show($id)
{
$offer = Offers::getFull($id);
dump($offer);
}
}

View File

@@ -86,6 +86,13 @@ class Article extends Model implements HasMedia
public function scopeWithOffers($query)
{
return $query->has('Offers');
return $query->has('offers');
}
public function scopeWithCurrentOffers($query)
{
return $query->whereHas('offers', function ($query) {
$query->where('status_id', 1);
});
}
}

View File

@@ -14,9 +14,11 @@ use Spatie\Translatable\HasTranslations;
use Rinvex\Tags\Traits\Taggable;
use Wildside\Userstamps\Userstamps;
use Kalnoy\Nestedset\NodeTrait;
class Category extends Model
{
use HasTranslations, InteractsWithMedia, SoftDeletes, Taggable, Userstamps;
use HasTranslations, InteractsWithMedia, NodeTrait, SoftDeletes, Taggable, Userstamps;
protected $guarded = ['id'];
protected $table = 'categories';
@@ -41,4 +43,9 @@ class Category extends Model
{
return $query->where('category_id', $category_id);
}
public function scopeVisible($query)
{
return $query->where('visible', 1);
}
}

View File

@@ -38,6 +38,11 @@ class Offer extends Model
return $this->belongsTo(Variation::class);
}
public function scopeActive($query)
{
return $query->where('status_id', 1);
}
public function scopeByArticle($query, $id)
{
return $query->where('article_id', $id);

View File

@@ -6,11 +6,6 @@ use App\Repositories\Core\Arrays;
class Categories
{
public static function getTree($withFolder = false)
{
$categories = self::getCategoryTree()->toArray();
return self::getChildren($categories[0]['children'], $withFolder);
}
public static function getFancyTree()
{
@@ -20,6 +15,12 @@ class Categories
return $categories;
}
public static function getTree($withFolder = false)
{
$categories = self::getCategoryTree()->toArray();
return self::getChildren($categories[0]['children'], $withFolder);
}
public static function getCategoryTree()
{
return self::getModel()->defaultOrder()->get()->toTree();

View File

@@ -59,6 +59,11 @@ class Articles
return $data;
}
public static function getArticlesWithOffers()
{
return Article::withCurrentOffers()->with('offers')->get();
}
public static function getFull($id)
{
$data['article'] = self::getArticleEdit($id);

View File

@@ -38,6 +38,11 @@ class Categories
return $category->tags->pluck('name', 'id')->toArray();
}
public static function getTreeVisibles()
{
$tree = Category::get()->toTree();
}
public static function getTree()
{
return CategoryTrees::getTree();

View File

@@ -19,7 +19,7 @@ class Offers
public static function getLast()
{
return Offer::with(['article.image'])->orderByDesc('updated_at')->get();
return Offer::with(['article.image'])->active()->orderByDesc('updated_at')->get();
}
public static function getByCategoryWithTags($category_id)