0

the query I wrote may at some point contain columns that are null which causes the query to return an empty array even though there is a value in the campaigns table. This is caused because the query cant make a join if the column values are null. I am trying to write an if statement to return the campaign even if its values are null but its still returning an empty array. If I run the query in the if statement it returns me what I am looking for I just cant get the if statement to trigger.

  public function getCampaigns(Request $request)
  {
    $authUser = auth()->user();
    $business = $authUser['business_id'];
    $query = campaigns::join('offers', 'campaigns.offer_id', '=', 'offers.id')
            ->join('conditionals', 'campaigns.conditional_id', '=', 'conditionals.id')
            ->join('triggers', 'campaigns.offer_id', '=', 'triggers.id')
            ->select('campaigns.*',
            'triggers.name AS trigger_name',
            'offers.name AS offer_name',
            'offers.reward_amount as offer_reward_amount',
            'offers.plaid_category_id as offer_plaid_id',
            'conditionals.dollars_min',
            'conditionals.dollars_max',
            'conditionals.dollars_more',
            'conditionals.xDays as days_since_last_purchase',
            'conditionals.xDate as date_since_last_purchase',
            'conditionals.xPercent as percentage_more',
            'conditionals.xPurchases as purchases_x_times',
            'conditionals.start_date',
            'conditionals.end_date',
            'conditionals.plaid_category_id')
            // ->whereNull('campaigns.trigger_id', 'campaigns.offer_id', 'campaigns.conditional_id')
            ->where('campaigns.business_id', '=', $business);
    if ($query->count() === 0){
      $query = campaigns::select('*')->where('business_id', '=', $business);
    }
    return $query->get();
  }
1

1 Answer 1

3

The query builder get() method will not return null if no results are found, it will return an empty collection.

Check for an empty collection instead:

if ($query->count() === 0){

I would recommend renaming $query for readability too, since it's not assigned the query, it's assigned the results.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the fast response. that worked. only problem I have is that if there is a campaign with no null values and one with null values it only shows the campaign with null values. I wrote a foreach loop around the if statement but that didnt work either.
which values are null? I'm sure that can be solved with your query instead of php.
The joined columns are null. It will need php. I can figure that out. thanks for the help with the query.
I think you should look into left joins

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.