0

Although there is no product in my relationships, it comes under the product price relationship.

I don't want it to come, I share my codes below.

Controller

 public function getProductListAll()
    {
        $products = Product::with(['property'=>function($q){
            $q->select(['features','for_who','product_id']);
        }])
            ->without('products')
            ->whereStatus(1)
            ->where('show', 1)
            ->where('code', '!=', 1)
            ->where('is_roketable', true)
            ->with(['prices_roket' => function ($q) {
                return $q->whereNull('user_id');
            }]);
        $products = $products->whereIn('product_type', ['education', 'consultancy', 'startup']);

        $data['products'] = $products->select([
            'id', 'name', 'image', 'commission', 'price',
            'buy_now', 'type', 'short_desc', 'pricing_desc',
            'offer_status', 'period_status', 'period', 'period_frequency',
            'endpoint', 'product_type', 'unique_id', 'discount_show_status',
            'annual_payment', 'pre_register', 'privacy_status', 'privacy',
            'sg_project', 'last_pre_register_date', 'product_desc',
            'product_consulting', 'product_status',
        ])->get();

        return response()->json($data);

    }

Model

    public function prices_roket()
    {
        return $this->hasMany(ProductPrice::class)->select(['id', 'product_id', 'price', 'user_id', 'offer_id'])
            ->orderBy('price')->whereNull('offer_id');
    }

https://prnt.sc/J65WxREoo4Fk

this is how the product

in this way I am trying to remove the product from the api but without success.

$data['products'] = $data['products']->map(function($product) {
    if(!$product->prices_roket) {
        return $product;
    }

    $product->prices_roket = $product->prices_roket
        ->map(function($pr) {
            $pr->forget('product');

            return $pr;
        });

    return $product;
});

Model Content

    protected $table = 'products';

    protected $fillable = [
        'id',
        'code',
        'name',
        'category_id',
        'description',
        'image',
        'commission',
        'price',
        'buy_now',
        'route',
        'btn_text',
        'type',
        'status',
        'short_desc',
        'pricing_desc',
        'author',
        'author_email',
        'author_web',
        'author_logo',
        'addon_id',
        'show',
        'discount_code_status',
        'discount',
        'discount_status',
        'offer_status',
        'period_status',
        'period',
        'period_frequency',
        'sort',
        'remaning_status',
        'action',
        'subscription',
        'key',
        'endpoint',
        'product_type',
        'unique_id',
        'discount_show_status',
        'annual_payment',
        'auto_assign_user_id',
        'relational_product_status',
        'supplier_id',
        'form_id',
        'contents',
        'auto_invoice',
        'pre_register',
        'privacy_status',
        'privacy',
        'sg_crm_code',
        'sg_supplier',
        'sg_project',
        'activated_at',
        'passived_at',
        'last_pre_register_date',
        'supplier_commision',
        'popup_show',
        'earn_money',
        'product_desc',
        'start_popup_time',
        'finish_popup_time',
        'product_consulting',
        'product_status',
        'banner_one',
        'banner_two',
        'banner_three',
        'banner_four',
        'is_roketable'
    ];

    protected $hidden = [
        'auto_assign_user_id'
    ];

    protected $dates = [];

    protected $casts = [
        'contents' => 'array'
    ];

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            if ($model->category_id) {
                $category = ProductCategory::find($model->category_id);
                if ($category) {
                    $model->type = $category->name;
                }
            }
            $model->code = initials($model->name) . '01';
        });

        static::updating(function ($model) {
            if ($model->category_id) {
                $category = ProductCategory::find($model->category_id);
                if ($category) {
                    $model->type = $category->name;
                }
            }
        });

        // static::addGlobalScope(new MyProductScope);
    }

    public function scopeMyProduct($query)
    {
        $user = auth()->user();
        if ($user && !$user->isAdmin() && $user->role != 'business') {
            $ids = DB::table('user_products')->where('user_id', $user->id)->pluck('product_id');
            $query->whereIn('products.id', $ids);
        }
    }

    protected $appends = [
        'image_url',
        'author_logo_url',
        'data',
        'education',
        'unique',
        'description_string'
    ];
``` and ProductPrice Model
protected $table = 'product_prices';

protected $fillable = [
    'price',
    'description',
    'product_id',
    'product_price_id',
    'user_id',
    'offer_id',
    'sort',
    'tax',
    'person_count_status',
    'person_price',
    'person_text',
    'free',
    'visibility',
    'currency',
    'meeting_required',
    'subscription_price',
    'annual_price',
    'is_analysis',
    'invoice_required',
    'product_question_group_id',
    'send_mail',
    'price_type',
    'discounted_price',
    'project_code',
    'person_count_accept',
    'foreign_web_link',
    'foreign_web_site_status',
    'portal_product_show_name',
    'edanisman_money',
    'reportable'
];

protected $casts = [
    'free'                => 'boolean',
    'person_count_status' => 'boolean',
    'meeting_required'    => 'boolean',
    'is_analysis'         => 'boolean',
    'invoice_required'    => 'boolean',
    'invoice_required'    => 'boolean',
    'reportable'           => 'boolean',
    // 'annual_price'        => 'decimal',
];

protected $dates = [];

protected $hidden = [];

protected $appends = [
    'real_price',
    'real_person_price',
    'main_price',
    'text_desc',
    'package_name',
    'remaning_price',
    'remaning_months',
    'discount_status',
    'discount_type',
    'annual_discount_rate',
    'currency_symbol',
    'currency_rate',
    'user_has_price',
];
mehmet
  • 19
  • 4
  • Can you show the fields for tables `ProductPrices` and `Products`? Depending on the content, your relationship definition might need some work. Please add it to the question :) – Techno Jan 17 '23 at 10:46
  • The Model method to unload a relation is `unsetRelation('product')` not `forget('product')` – N69S Jan 17 '23 at 10:52
  • I've attached my whole model file. – mehmet Jan 17 '23 at 10:58
  • 1
    *"Although there is no product in my relationships"* - i'm not sure what do you meant by this, but you specifically queries `Product::with`. its kinda hard to make head or tails of your problem. perhaps you could reword/reorganize the code. its too much and idk what are you trying to do. it would be nice to have [mcve]. and oh, you can use `toSql()` or the `Database::getQueryLog` to see the actual query you get. – Bagus Tesa Jan 17 '23 at 10:59
  • @BagusTesa I actually wanted to say that my Product query comes twice. The first one is at the top and the other one is under my price relationship. – mehmet Jan 17 '23 at 11:00
  • @mehmet afaik the ORM will automatically links `price_roket` to `product` if the `product`s already loaded in memory. though, you might want to try to "hide" it by selecting only the required field using [`select()`](https://stackoverflow.com/a/57028386) when you call `with('prices_roket')` - idk if this still works though. – Bagus Tesa Jan 17 '23 at 11:22
  • @BagusTesa In fact, I have limited all fields, I cannot find where this product relationship comes from. – mehmet Jan 17 '23 at 11:26

0 Answers0