vendredi 22 mai 2020

Return relation if it counts greater than 0 else return other relation

I want to return a relation if it has results greater than 0 otherwise it should return other relation

public function default_product_image()
    {  
        if(count($this->ztecpc_product_images()->get()) > 0)
        {
            return $this->hasMany('App\ZtecpcProductImage','product_id','id')//if count > 0 then return this relation
                    ->select('id','product_id','image');
        }
        return $this->hasOne('App\ProductImagesModel','product_id','id')//else return this relation
                    ->where('main_image',1)
                    ->select('id','product_id','image');
    }

 public function ztecpc_product_images()
    {
        return $this->hasMany('App\ZtecpcProductImage','product_id','id')
                    ->select('id','product_id','image');
    }

I want to return return $this->hasMany('App\ZtecpcProductImage','product_id','id') ->select('id','product_id','image'); if ztecpc_product_images relation has the results greater than 0 otherwise it should return other relation in the function. My problem is that it is always return empty array which I guess it the if-condition if(count($this->ztecpc_product_images()->get()) > 0) is always true.



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

Aucun commentaire:

Enregistrer un commentaire