mercredi 22 décembre 2021

Can I use a Models scope with Laravel's 5.5 Query Builder Join method?

I am trying to use Laravel 5.5 Query Builders Join method with a scope method from the CommercialCoupon Model but when I try to apply this scope I get this error:

[2021-12-22 23:18:16] lumen.ERROR: BadMethodCallException: Call to undefined method Illuminate\Database\Query\JoinClause::filterBlockedCoupons() in /var/www/new-api/vendor/illuminate/database/Query/Builder.php:2483

My CommercialCoupon Model has this scope:

public function scopeFilterBlockedCoupons($query, $channelPartnerId){
    $query->whereNotIn('discount_provider_id', function($subquery) use ($channelPartnerId)
        {
            $subquery->select('discount_providers.id')
            ->from('discount_providers')
            ->join('discount_provider_permissions', function($join)
            {
                $join->on('discount_provider_permissions.discount_provider_id', '=', 'discount_providers.id' );
                $join->on('discount_providers.block_type', '=', \DB::raw(Config::get('systemtype.discount_provider_block_types.deny')));
            })
            ->where('discount_provider_permissions.channel_partner_id', '=', \DB::raw($channelPartnerId));
        })
    ->whereNotIn('discount_provider_id',  function($subquery) use ($channelPartnerId)
        {
            $subquery->select(\DB::raw('
                CASE WHEN COUNT(discount_provider_permissions.id) > 0 then
                    -1
                ELSE
                    discount_providers.id
                END
                AS excludedDiscountProviderId'))
            ->from('discount_providers')
            ->leftJoin('discount_provider_permissions', function($join) use ($channelPartnerId)
            {
                $join->on('discount_provider_permissions.discount_provider_id', '=', 'discount_providers.id' );
                $join->on('discount_provider_permissions.channel_partner_id', '=', \DB::raw($channelPartnerId) );
            })
        ->where('discount_providers.block_type', '=', \DB::raw(Config::get('systemtype.discount_provider_block_types.allow')))
        ->groupBy('discount_providers.id');
    });
}

This scope works perfectly if I'm calling the CommercialCoupon Model directly.

When I try to use this scope inside of a join displayed in the code below I get the error above:

$coupons = Carousel::where('carousels.channel_partner_id', '=', $selectedChannelPartnerId)
    ->join('carousel_contents', 'carousels.id', '=', 'carousel_contents.carousel_id')
    ->join('commercial_coupons', function ($join) use ($channelPartnerId) {
        $join->on('commercial_coupons.id', '=', 'carousel_contents.table_id')
            ->filterBlockedCoupons($channelPartnerId);
    })
    ->join('discount_providers', 'discount_providers.id', '=', 'commercial_coupons.discount_provider_id')
    ->where('carousel_contents.table_name', '=', \DB::raw("'commercial_coupons'"))
    ->select("commercial_coupons.logo_image AS marketing_image", "commercial_coupons.id AS coupon_id", "discount_providers.name as discount_provider_name", "commercial_coupons.coupon_org_name as coupon_title", "commercial_coupons.discount_provider_id")
    ->orderBy('carousel_contents.order')
    ->get();

Am I able to use the scope on the Model or do I just have to copy and paste the code inside the scope this specific join?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3mo7zkX
via IFTTT

Aucun commentaire:

Enregistrer un commentaire