جشنواره عیدانه راکت | عضویت ویژه راکت برای آخرین بار | افزایش قیمت‌ها از سال جدید | و ...

مشاهده اطلاعات بیشتر...
ثانیه
دقیقه
ساعت
روز
webazin webazin
4 سال پیش توسط webazin webazin مطرح شد
7 پاسخ

hasManyThrough در جدول دسته بندی چند سطحی

hasManyThrough در جدول دسته بندی چند سطحی به چه شکل باید انجام بشه؟

الان دسته بندی های من سه سطحی هست،و واسه سطح یک با جدول محصولات از این پکیج استفاده کردم
https://github.com/staudenmeir/eloquent-has-many-deep

واسه سطح دو با محصولات به چه شکل میشه؟کسی کار کرده؟
این ارور رو میده

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'categories' (SQL: select count(*) as aggregate from `categories` inner join `categories` on `categories`.`id` = `categories`.`parent_id` where `categories`.`parent_id` = 2)

ثبت پرسش جدید
webazin webazin
@webazin.org90 4 سال پیش مطرح شد
0

@ali.bayat

<?php

namespace App\Models;

use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Support\Facades\DB;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
use Staudenmeir\EloquentHasManyDeep\HasTableAlias;

class Category extends Model
{
    use Sluggable, HasRelationships, HasTableAlias;

    protected $fillable = ['title', 'slug', 'seo_tag', 'seo_description', 'parent_id'];

    public const IMAGE_SIZE = ['40', '100', '200'];

    /**
     * relation category with parent category.
     * @return BelongsTo
     */
    public function parent()
    {
        return $this->belongsTo(Category::class, 'parent_id');
    }

    /**
     * relation category with children category.
     * @return HasMany
     */
    public function children()
    {
        return $this->hasMany(Category::class, 'parent_id');
    }

    /**
     * relation category with allChildren category.
     * @return HasMany
     */
    public function allChildren()
    {
        return $this->children()->with('allChildren');
    }

    /**
     * relation category with allParent category.
     * @return BelongsTo
     */
    public function allParent()
    {
        return $this->parent()->with('allParent');
    }

    /**
     * check category has parent.
     * @return bool
     */
    public function hasParent()
    {
        return !!$this->parent;
    }

    /**
     * check category has children.
     * @return bool
     */
    public function hasChildren()
    {
        return !!$this->children->count();
    }

    /**
     * relation of great grandfather(level 1) category with products.
     * @return HasManyDeep
     */
    public function deepProducts()
    {
        return $this->hasManyDeep(Product::class, ['App\Models\Category as alias', Category::class], [
            'parent_id', // Foreign key on the "category" table.
            'parent_id',    // Foreign key on the "category" table.
            'category_id'     // Foreign key on the "product" table.
        ], [
            'id', // Local key on the "categories" table.
            'id', // Local key on the "categories" table.
            'id'  // Local key on the "posts" table.
        ]);
    }

    /**
     * relation grandfather(level 2) with products.
     * @return HasManyThrough
     */
    public function throughProducts()
    {
        return $this->hasManyDeep(Product::class, ['App\Models\Category as alias'], ['parent_id', 'category_id']);
    }

    /**
     * relation father(level 3) with products.
     * @return HasMany
     */
    public function manyProducts()
    {
        return $this->hasMany(Product::class);
    }

    /**
     * relation category with image morph one to one.
     * @return MorphOne
     */
    public function image()
    {
        return $this->morphOne(Image::class, 'imageable');
    }

    /**
     * url param with slug instead id attribute.
     * @return string
     */
    public function getRouteKeyName()
    {
        return 'slug';
    }

    /**
     * Return the sluggable configuration array for this model.
     * create slug by title attribute.
     * @return array
     */
    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }
}

حل شد،با همون پکیج حل شد،از خود نویسندش به زور پرسیدم:))))))
اینم میذارم شاید کسی گیر کرد


علی بیات
تخصص : توسعه دهنده ارشد وب
@ali.bayat 4 سال پیش مطرح شد
1

درود
من با این پکیج کار نکردم اما مستندات بدی هم نداره.

اگر قصد شما پیاده سازی دسته بندی‌های ساده و تو در تو هست... میتونید از پکیج Laravel-Categorizable هم استفاده کنید. توضیحات کامل در گفتگوی زیر:
https://roocket.ir/discuss/6117


webazin webazin
@webazin.org90 4 سال پیش آپدیت شد
1

@ali.bayat
ممنون از شما،پکیج خوبی نوشتید،ولی الان من خیلی از کارم گذشته و بخوام از اول این پکیج رو پیاده سازی کنم وقت میبره،همین رابطه رو نمیشه پیاده کرد؟یه جوری باید اسم تیبل تغییر کنه تا ارور نده،ینی استفاده از الیاس ها، ممنون میشم کمک کنید


علی بیات
تخصص : توسعه دهنده ارشد وب
@ali.bayat 4 سال پیش مطرح شد
1

چند نکته:

  • ۱. رابطه BelongsToMany که در کدتون دارید رو به اشتراک بگذارید.. به اشتراک گذاشتن کدهایی که دارید باعث میشه دوستان بهتر بتونند کمک کنند

  • ۲. یک رابطه چند به چند نیاز به pivot داره. پس چک کنید که آیا از پیوِت استفاده کردید یا نه؟

  • ۳. در مورد alias در کوئری SQL
    کوئری شما به شکل زیره:

    select count(*) as aggregate 
    from `categories` inner join `categories` 
    on `categories`.`id` = `categories`.`parent_id` 
    where `categories`.`parent_id` = 2)

در صورتی که باید شبیه به زیر باشه تا alias ها درست رعایت شده باشند:

select count(*) as aggregate 
from `categories` c1 inner join `categories` c2
on c1.`id` = c2.`parent_id` 
where c1.`parent_id` = 2)

چنانچه از قبل موارد ۲ و ۳ رو رعایت کرده اید: مشکل میتونه از توابعی باشه که رابطه ها رو درونشون تعریف کردید.


webazin webazin
@webazin.org90 4 سال پیش مطرح شد
0

@ali.bayat
سلام دوست گرامی،تشکر از پاسختون
این روابط من هست

/**
     * relation category with parent category.
     * @return BelongsTo
     */
    public function parent()
    {
        return $this->belongsTo(Category::class, 'parent_id');
    }

    /**
     * relation category with children category.
     * @return HasMany
     */
    public function children()
    {
        return $this->hasMany(Category::class, 'parent_id');
    }

/**
     * relation of grandpa(level 1) category with products.
     * @return HasManyDeep
     */
    public function deepProducts()
    {
        return $this->hasManyDeep(Product::class, ['App\Models\Category as alias', Category::class], [
            'parent_id', // Foreign key on the "category" table.
            'parent_id',    // Foreign key on the "category" table.
            'category_id'     // Foreign key on the "product" table.
        ], [
            'id', // Local key on the "countries" table.
            'id', // Local key on the "users" table.
            'id'  // Local key on the "posts" table.
        ]);
    }

    /**
     * relation father(level 2) with products.
     * @return HasManyThrough
     */
    public function throughProducts()
    {
        return $this->hasManyThrough(Category::class, Category::class, 'parent_id', 'parent_id');
    }

دسته بندی ها همشون رابطه یک به چند دارن،ولی رابطه سطح دو با محصولات که با through هستش رو ارور میده


webazin webazin
@webazin.org90 4 سال پیش مطرح شد
0

@ali.bayat

<?php

namespace App\Models;

use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Support\Facades\DB;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
use Staudenmeir\EloquentHasManyDeep\HasTableAlias;

class Category extends Model
{
    use Sluggable, HasRelationships, HasTableAlias;

    protected $fillable = ['title', 'slug', 'seo_tag', 'seo_description', 'parent_id'];

    public const IMAGE_SIZE = ['40', '100', '200'];

    /**
     * relation category with parent category.
     * @return BelongsTo
     */
    public function parent()
    {
        return $this->belongsTo(Category::class, 'parent_id');
    }

    /**
     * relation category with children category.
     * @return HasMany
     */
    public function children()
    {
        return $this->hasMany(Category::class, 'parent_id');
    }

    /**
     * relation category with allChildren category.
     * @return HasMany
     */
    public function allChildren()
    {
        return $this->children()->with('allChildren');
    }

    /**
     * relation category with allParent category.
     * @return BelongsTo
     */
    public function allParent()
    {
        return $this->parent()->with('allParent');
    }

    /**
     * check category has parent.
     * @return bool
     */
    public function hasParent()
    {
        return !!$this->parent;
    }

    /**
     * check category has children.
     * @return bool
     */
    public function hasChildren()
    {
        return !!$this->children->count();
    }

    /**
     * relation of great grandfather(level 1) category with products.
     * @return HasManyDeep
     */
    public function deepProducts()
    {
        return $this->hasManyDeep(Product::class, ['App\Models\Category as alias', Category::class], [
            'parent_id', // Foreign key on the "category" table.
            'parent_id',    // Foreign key on the "category" table.
            'category_id'     // Foreign key on the "product" table.
        ], [
            'id', // Local key on the "categories" table.
            'id', // Local key on the "categories" table.
            'id'  // Local key on the "posts" table.
        ]);
    }

    /**
     * relation grandfather(level 2) with products.
     * @return HasManyThrough
     */
    public function throughProducts()
    {
        return $this->hasManyDeep(Product::class, ['App\Models\Category as alias'], ['parent_id', 'category_id']);
    }

    /**
     * relation father(level 3) with products.
     * @return HasMany
     */
    public function manyProducts()
    {
        return $this->hasMany(Product::class);
    }

    /**
     * relation category with image morph one to one.
     * @return MorphOne
     */
    public function image()
    {
        return $this->morphOne(Image::class, 'imageable');
    }

    /**
     * url param with slug instead id attribute.
     * @return string
     */
    public function getRouteKeyName()
    {
        return 'slug';
    }

    /**
     * Return the sluggable configuration array for this model.
     * create slug by title attribute.
     * @return array
     */
    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }
}

حل شد،با همون پکیج حل شد،از خود نویسندش به زور پرسیدم:))))))
اینم میذارم شاید کسی گیر کرد


علی بیات
تخصص : توسعه دهنده ارشد وب
@ali.bayat 4 سال پیش مطرح شد
0

درود

پس مشکل از متد throughProducts بود که باید به مدل Product پوینت میکرد و نه به خود Category :)

پیروز باشید


webazin webazin
@webazin.org90 4 سال پیش آپدیت شد
0

@ali.bayat
بله، مشکل همین بود:))
ولی این پکیجه خیلی خوب بود، حال داد

البته این تریت این کارو انجام میده ها

HasTableAlias


برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام