Add deep relations

This commit is contained in:
Ludovic CANDELLIER
2022-01-14 00:03:21 +01:00
parent 95ca3c6404
commit 050fd76122
13 changed files with 159 additions and 30 deletions

View File

@@ -19,7 +19,9 @@ class HomeController extends Controller
public function index() public function index()
{ {
$data = self::init(); $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); dump($data);
exit; exit;
return view('Shop.home', $data); return view('Shop.home', $data);

View File

@@ -11,13 +11,14 @@ use Rinvex\Tags\Traits\Taggable;
use Kirschbaum\PowerJoins\PowerJoins; use Kirschbaum\PowerJoins\PowerJoins;
use Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin; use Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin;
use Wildside\Userstamps\Userstamps; use Wildside\Userstamps\Userstamps;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
use App\Traits\Model\HasComments; use App\Traits\Model\HasComments;
use App\Traits\Model\Imageable; use App\Traits\Model\Imageable;
class Article extends Model implements HasMedia 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 $guarded = ['id'];
protected $table = 'shop_articles'; protected $table = 'shop_articles';
@@ -37,10 +38,12 @@ class Article extends Model implements HasMedia
return $this->hasMany(Offer::class); return $this->hasMany(Offer::class);
} }
/*
public function prices() public function prices()
{ {
return $this->hasMany(Price::class); return $this->hasManyDeep(PriceListValue::class, [Offer::class, Tariff::class, PriceList::class]);
} }
*/
public function product() public function product()
{ {
@@ -52,6 +55,12 @@ class Article extends Model implements HasMedia
return $this->morphToMany(Tag::class, 'taggable'); 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) public function scopeByArticle($query, $id)
{ {
return $query->where($this->table . '.id', $id); return $query->where($this->table . '.id', $id);
@@ -89,10 +98,15 @@ class Article extends Model implements HasMedia
return $query->has('offers'); return $query->has('offers');
} }
public function scopeWithCurrentOffers($query) public function scopeWithAvailableOffers($query)
{ {
return $query->whereHas('offers', function ($query) { return $query->whereHas('offers', function ($query) {
$query->where('status_id', 1); $query->where('status_id', 1);
}); });
} }
public function scopeVisible($query)
{
return $query->where($this->table . '.visible', 1);
}
} }

View File

@@ -18,16 +18,6 @@ class Offer extends Model
return $this->belongsTo(Article::class); return $this->belongsTo(Article::class);
} }
public function categories()
{
return $this->article->categories();
}
public function tags()
{
return $this->article->tags();
}
public function tariff() public function tariff()
{ {
return $this->belongsTo(Tariff::class); return $this->belongsTo(Tariff::class);
@@ -38,14 +28,54 @@ class Offer extends Model
return $this->belongsTo(Variation::class); 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) public function scopeActive($query)
{ {
return $query->where('status_id', 1); return $query->where($this->table . '.status_id', 1);
} }
public function scopeByArticle($query, $id) 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) 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) public function scopeByTag($query, $tag_id)
{ {
return $query->whereHas('article.tags', function ($query) use ($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);
}
} }

View File

@@ -20,6 +20,11 @@ class PriceList extends Model
return $this->belongsTo(Tariff::class); return $this->belongsTo(Tariff::class);
} }
public function offers()
{
return $this->hasManyThrough(Offer::class, Tariff::class, 'id', 'tariff_id', 'id', 'id');
}
public function sale_channel() public function sale_channel()
{ {
return $this->belongsTo(SaleChannel::class); return $this->belongsTo(SaleChannel::class);
@@ -44,4 +49,11 @@ class PriceList extends Model
{ {
return $query->where($this->table . '.status_id', $id); return $query->where($this->table . '.status_id', $id);
} }
public function scopeByOffer($query, $id)
{
return $query->whereHas('offers', function ($query) use ($id) {
$query->byOffer($id);
});
}
} }

View File

@@ -20,6 +20,20 @@ class PriceListValue extends Model
return $this->belongsTo(PriceList::class); 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) public function scopeByPriceList($query, $id)
{ {
return $query->where($this->table . '.price_list_id', $id); return $query->where($this->table . '.price_list_id', $id);

View File

@@ -15,6 +15,11 @@ class Tariff extends Model
protected $guarded = ['id']; protected $guarded = ['id'];
protected $table = 'shop_tariffs'; protected $table = 'shop_tariffs';
public function offers()
{
return $this->hasMany(Offer::class);
}
public function sale_channel() public function sale_channel()
{ {
@@ -37,6 +42,11 @@ class Tariff extends Model
return $this->hasMany(PriceList::class); return $this->hasMany(PriceList::class);
} }
public function price_list_values()
{
return $this->hasManyThrough(PriceListValue::class, PriceList::class);
}
public function scopeByAutocomplete($query, $str) public function scopeByAutocomplete($query, $str)
{ {
return $query->where($this->table . '.name', 'LIKE', "%${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); 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);
});
}
} }

View File

@@ -19,7 +19,7 @@ class Articles
public static function autocomplete($str) 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 = []; $export = [];
foreach ($data as $key => $name) { foreach ($data as $key => $name) {
$export[] = ['value' => $key, 'text' => $name]; $export[] = ['value' => $key, 'text' => $name];
@@ -59,9 +59,20 @@ class Articles
return $data; 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() 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) public static function getFull($id)

View File

@@ -11,6 +11,11 @@ use App\Models\Shop\PriceList;
class PriceLists class PriceLists
{ {
public static function getByOffer($offer_id)
{
return PriceList::byOffer($offer_id)->get();
}
public static function getStatus($status_id) public static function getStatus($status_id)
{ {
return self::getStatuses()[$status_id]; return self::getStatuses()[$status_id];

View File

@@ -16,6 +16,11 @@ class Tariffs
return $export; return $export;
} }
public static function getByOffer($id)
{
return Tariff::byOffer($id)->first();
}
public static function getPrices($id) public static function getPrices($id)
{ {
return Tariff::with(['price_lists.price_list_values', 'price_lists.sale_channel'])->find($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() public static function getStatuses()
{ {
return ['Actif','Suspendu','Invisible','Obsolete']; return ['Actif', 'Suspendu', 'Invisible', 'Obsolete'];
} }
public static function getAll() public static function getAll()

View File

@@ -41,6 +41,7 @@
"jenssegers/date": "^4.0", "jenssegers/date": "^4.0",
"kalnoy/nestedset": "^5.0", "kalnoy/nestedset": "^5.0",
"kirschbaum-development/eloquent-power-joins": "^2.3", "kirschbaum-development/eloquent-power-joins": "^2.3",
"kmlaravel/laravel-geographical-calculator": "^2.1",
"knplabs/knp-snappy": "^1.2", "knplabs/knp-snappy": "^1.2",
"laracasts/utilities": "^3.0", "laracasts/utilities": "^3.0",
"laravel/framework": "^8.42", "laravel/framework": "^8.42",
@@ -79,6 +80,7 @@
"spatie/laravel-backup": "^6.16", "spatie/laravel-backup": "^6.16",
"spatie/laravel-mail-preview": "^4.0", "spatie/laravel-mail-preview": "^4.0",
"spatie/laravel-medialibrary": "^9.6", "spatie/laravel-medialibrary": "^9.6",
"spatie/laravel-stats": "^1.0",
"staudenmeir/belongs-to-through": "^2.11", "staudenmeir/belongs-to-through": "^2.11",
"staudenmeir/eloquent-has-many-deep": "^1.13", "staudenmeir/eloquent-has-many-deep": "^1.13",
"stillat/numeral.php": "^2.0", "stillat/numeral.php": "^2.0",

View File

@@ -118,6 +118,7 @@
"jstree": "^3.3.11", "jstree": "^3.3.11",
"jszip": "^3.6.0", "jszip": "^3.6.0",
"laravel-echo": "^1.8.1", "laravel-echo": "^1.8.1",
"list.js": "^2.3.1",
"modernizr": "^3.11.3", "modernizr": "^3.11.3",
"moment": "^2.27.0", "moment": "^2.27.0",
"morris.js": "^0.5.0", "morris.js": "^0.5.0",

View File

@@ -0,0 +1,25 @@
<a href="{{ route('Shop.Articles.show', ['id' => $article['id']]) }}">
<div class="card">
<img src="{{ App\Repositories\Shop\Articles::getPreviewSrc($article['image'] ?? false) }}" class="card-img-top" alt="...">
<div class="card-body">
<span class="card-title">{{ $article['name'] }}</span>
<span class="pull-right">
<i class="fa fa-heart"></i>
</span>
<p class="card-text">
<div class="row">
<div class="col-6">
3.50 <br>
Semence
</div>
<div class="col-6">
1.65 <br>
Plant
</div>
</div>
</p>
</div>
</div>
</a>

View File

@@ -1,7 +1,7 @@
<div class="row"> <div class="row">
@foreach ($offers as $offer) @foreach ($articles as $article)
<div class="col-sm-3 col-lg-2"> <div class="col-sm-3 col-lg-2">
@include('Shop.Offers.offer') @include('Shop.Articles.partials.article')
</div> </div>
@endforeach @endforeach
</div> </div>