Add deep relations
This commit is contained in:
@@ -19,7 +19,9 @@ class HomeController extends Controller
|
||||
public function index()
|
||||
{
|
||||
$data = self::init();
|
||||
$data['offers'] = Offers::getLast()->toArray();
|
||||
// $data['offers'] = Offers::getLast()->toArray();
|
||||
$data['articles'] = Articles::getArticlesWithOffers()->toArray();
|
||||
// $data['prices'] = $data['articles']['offers'][0]['tariff']['price_lists'][0]['price_list_values'][0];
|
||||
dump($data);
|
||||
exit;
|
||||
return view('Shop.home', $data);
|
||||
|
||||
@@ -11,13 +11,14 @@ 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, Imageable, Powerjoins, Taggable, SoftDeletes, UserStamps;
|
||||
use Categorizable, EloquentJoin, HasComments, HasRelationships, Imageable, Powerjoins, Taggable, SoftDeletes, UserStamps;
|
||||
|
||||
protected $guarded = ['id'];
|
||||
protected $table = 'shop_articles';
|
||||
@@ -37,10 +38,12 @@ class Article extends Model implements HasMedia
|
||||
return $this->hasMany(Offer::class);
|
||||
}
|
||||
|
||||
/*
|
||||
public function prices()
|
||||
{
|
||||
return $this->hasMany(Price::class);
|
||||
return $this->hasManyDeep(PriceListValue::class, [Offer::class, Tariff::class, PriceList::class]);
|
||||
}
|
||||
*/
|
||||
|
||||
public function product()
|
||||
{
|
||||
@@ -52,6 +55,12 @@ class Article extends Model implements HasMedia
|
||||
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);
|
||||
@@ -89,10 +98,15 @@ class Article extends Model implements HasMedia
|
||||
return $query->has('offers');
|
||||
}
|
||||
|
||||
public function scopeWithCurrentOffers($query)
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,16 +18,6 @@ class Offer extends Model
|
||||
return $this->belongsTo(Article::class);
|
||||
}
|
||||
|
||||
public function categories()
|
||||
{
|
||||
return $this->article->categories();
|
||||
}
|
||||
|
||||
public function tags()
|
||||
{
|
||||
return $this->article->tags();
|
||||
}
|
||||
|
||||
public function tariff()
|
||||
{
|
||||
return $this->belongsTo(Tariff::class);
|
||||
@@ -38,14 +28,54 @@ class Offer extends Model
|
||||
return $this->belongsTo(Variation::class);
|
||||
}
|
||||
|
||||
public function price_lists()
|
||||
{
|
||||
return $this->hasManyThrough(PriceList::class, Tariff::class, 'id', 'tariff_id', 'id', 'id');
|
||||
}
|
||||
|
||||
public function categories()
|
||||
{
|
||||
return $this->article->categories();
|
||||
}
|
||||
|
||||
public function tags()
|
||||
{
|
||||
return $this->article->tags();
|
||||
}
|
||||
|
||||
public function get_price_lists()
|
||||
{
|
||||
return $this->tariff->price_lists();
|
||||
}
|
||||
|
||||
public function get_price_list_values()
|
||||
{
|
||||
return $this->tariff->price_lists->price_list_values();
|
||||
}
|
||||
|
||||
public function scopeActive($query)
|
||||
{
|
||||
return $query->where('status_id', 1);
|
||||
return $query->where($this->table . '.status_id', 1);
|
||||
}
|
||||
|
||||
public function scopeByArticle($query, $id)
|
||||
{
|
||||
return $query->where('article_id', $id);
|
||||
return $query->where($this->table . '.article_id', $id);
|
||||
}
|
||||
|
||||
public function scopeByOffer($query, $id)
|
||||
{
|
||||
return $query->where($this->table . '.id', $id);
|
||||
}
|
||||
|
||||
public function scopeByStatus($query, $id)
|
||||
{
|
||||
return $query->where($this->table . '.status_id', $id);
|
||||
}
|
||||
|
||||
public function scopeByVariation($query, $id)
|
||||
{
|
||||
return $query->where($this->table . '.variation_id', $id);
|
||||
}
|
||||
|
||||
public function scopeByArticleNature($query, $article_nature_id)
|
||||
@@ -69,11 +99,6 @@ class Offer extends Model
|
||||
});
|
||||
}
|
||||
|
||||
public function scopeByStatus($query, $id)
|
||||
{
|
||||
return $query->where('status_id', $id);
|
||||
}
|
||||
|
||||
public function scopeByTag($query, $tag_id)
|
||||
{
|
||||
return $query->whereHas('article.tags', function ($query) use ($tag_id) {
|
||||
@@ -88,8 +113,4 @@ class Offer extends Model
|
||||
});
|
||||
}
|
||||
|
||||
public function scopeByVariation($query, $id)
|
||||
{
|
||||
return $query->where('variation_id', $id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,11 @@ class PriceList extends Model
|
||||
return $this->belongsTo(Tariff::class);
|
||||
}
|
||||
|
||||
public function offers()
|
||||
{
|
||||
return $this->hasManyThrough(Offer::class, Tariff::class, 'id', 'tariff_id', 'id', 'id');
|
||||
}
|
||||
|
||||
public function sale_channel()
|
||||
{
|
||||
return $this->belongsTo(SaleChannel::class);
|
||||
@@ -44,4 +49,11 @@ class PriceList extends Model
|
||||
{
|
||||
return $query->where($this->table . '.status_id', $id);
|
||||
}
|
||||
|
||||
public function scopeByOffer($query, $id)
|
||||
{
|
||||
return $query->whereHas('offers', function ($query) use ($id) {
|
||||
$query->byOffer($id);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,20 @@ class PriceListValue extends Model
|
||||
return $this->belongsTo(PriceList::class);
|
||||
}
|
||||
|
||||
public function tariff()
|
||||
{
|
||||
return $this->belongsToThrough(
|
||||
'App\Models\Shop\Tariff',
|
||||
'App\Models\Shop\PriceList',
|
||||
null,
|
||||
'',
|
||||
[
|
||||
'App\Models\Shop\Tariff' => 'tariff_id',
|
||||
'App\Models\Shop\PriceList' => 'price_list_id'
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function scopeByPriceList($query, $id)
|
||||
{
|
||||
return $query->where($this->table . '.price_list_id', $id);
|
||||
|
||||
@@ -15,6 +15,11 @@ class Tariff extends Model
|
||||
|
||||
protected $guarded = ['id'];
|
||||
protected $table = 'shop_tariffs';
|
||||
|
||||
public function offers()
|
||||
{
|
||||
return $this->hasMany(Offer::class);
|
||||
}
|
||||
|
||||
public function sale_channel()
|
||||
{
|
||||
@@ -37,6 +42,11 @@ class Tariff extends Model
|
||||
return $this->hasMany(PriceList::class);
|
||||
}
|
||||
|
||||
public function price_list_values()
|
||||
{
|
||||
return $this->hasManyThrough(PriceListValue::class, PriceList::class);
|
||||
}
|
||||
|
||||
public function scopeByAutocomplete($query, $str)
|
||||
{
|
||||
return $query->where($this->table . '.name', 'LIKE', "%${str}%")
|
||||
@@ -53,4 +63,11 @@ class Tariff extends Model
|
||||
{
|
||||
return $query->where($this->table . '.status_id', $id);
|
||||
}
|
||||
|
||||
public function scopeByOffer($query, $id)
|
||||
{
|
||||
return $query->whereHas('offers', function ($query) use ($id) {
|
||||
$query->where('id', $id);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ class Articles
|
||||
|
||||
public static function autocomplete($str)
|
||||
{
|
||||
$data = Article::byAutocomplete($str)->orderBy('name')->limit(30)->get()->pluck('name', 'id');
|
||||
$data = Article::byAutocomplete($str)->orderBy('name')->limit(30)->pluck('name', 'id');
|
||||
$export = [];
|
||||
foreach ($data as $key => $name) {
|
||||
$export[] = ['value' => $key, 'text' => $name];
|
||||
@@ -59,9 +59,20 @@ class Articles
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function getArticlesToSell()
|
||||
{
|
||||
$articles = self::getArticlesWithOffers();
|
||||
foreach ($articles as $article) {
|
||||
$data[$article->article_nature->name][$article->name][] = [
|
||||
'description' => $article->description,
|
||||
];
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function getArticlesWithOffers()
|
||||
{
|
||||
return Article::withCurrentOffers()->with('offers')->get();
|
||||
return Article::visible()->withAvailableOffers()->with(['image', 'article_nature', 'offers.tariff.price_lists.price_list_values'])->get();
|
||||
}
|
||||
|
||||
public static function getFull($id)
|
||||
|
||||
@@ -11,6 +11,11 @@ use App\Models\Shop\PriceList;
|
||||
class PriceLists
|
||||
{
|
||||
|
||||
public static function getByOffer($offer_id)
|
||||
{
|
||||
return PriceList::byOffer($offer_id)->get();
|
||||
}
|
||||
|
||||
public static function getStatus($status_id)
|
||||
{
|
||||
return self::getStatuses()[$status_id];
|
||||
|
||||
@@ -16,6 +16,11 @@ class Tariffs
|
||||
return $export;
|
||||
}
|
||||
|
||||
public static function getByOffer($id)
|
||||
{
|
||||
return Tariff::byOffer($id)->first();
|
||||
}
|
||||
|
||||
public static function getPrices($id)
|
||||
{
|
||||
return Tariff::with(['price_lists.price_list_values', 'price_lists.sale_channel'])->find($id);
|
||||
@@ -33,7 +38,7 @@ class Tariffs
|
||||
|
||||
public static function getStatuses()
|
||||
{
|
||||
return ['Actif','Suspendu','Invisible','Obsolete'];
|
||||
return ['Actif', 'Suspendu', 'Invisible', 'Obsolete'];
|
||||
}
|
||||
|
||||
public static function getAll()
|
||||
|
||||
Reference in New Issue
Block a user