1
votes
$query = LeadsModel::with(
                            array(
                                    'emails' => function ($q) use ($inputs) 
                                    {
                                        $q->orwhere('email','like',"%john%");
                                        $q->orwhere('email','like',"%mark%");
                                    }
                                )
                            )
                            ->whereHas
                            (
                                'emails', function ($q) use ($inputs) 
                                {
                                    $q->orwhere('email','like',"%john%");
                                    $q->orwhere('email','like','%mark%');
                                }
                            );

    $res = $query->get();

    display_output($res);
    return;

The code above only returns all record and no condition applied to the result. I am not sure what is wrong. What I want is to add conditions in the query for the child table associated with the parent but will return the parent and child data where the condition is applied

additional question:

  • How can I add several OR condition in nested condition in eager loading?
  • how can I control the precedence of the condition? lets say I will have 2 or 3 condition in the same precedence and I will have another condition with AND condition in a nested eager loading condition

updated

This still records all data, no condition applied.

    $query = LeadsModel::with(
                            array(
                                    'emails' => function ($q) use ($inputs) 
                                    {
                                        $q->orWhere('email','like',"%john%");
                                        $q->orWhere('email','like',"%james%");
                                    }
                                )
                            )
                            ->whereHas
                            (
                                'emails', function ($q) use ($inputs) 
                                {
                                    $q->where('email','like',"%john%")->orWhere('email','like','%james%');
                                }
                            );

    $res = $query->get();

    display_output($res);
    return;

in normal sql i guess it would be

select leads.,lead_detail_emails. from leads, lead_detail_emails where leads.id = lead_detail_emails.lead_id and (lead_detail_emails.email like '%john%' OR lead_detail_emails.email like '%james%');

1
You should add some example/code what you want to achieve - Marcin Nabiałek
I have updated my question. thank you - rfpdl
Please explain what exactly records you want to get from database - now it seems you want to get leadsmodel records that have emails like john or james and get for them related model emails that have also only name john or james. - Marcin Nabiałek
i would like to get records from lead (parent of the email table) with condition of searching for john or james email - rfpdl
@Ponce Your guess about the query is wrong, check my edited answer for details. - Jarek Tkaczyk

1 Answers

4
votes

You need nested where:

whereHas('emails', function ($q)
{
    $q->where(function ($q) {
      $q->where('email','like',"%john%")
        ->orWhere('email','like','%mark%');
    });
})

This is example query for whereHas with pivot table (belongsToMany):

select * from `table` 
    where `table`.`deleted_at` is null 
        and (select count(*) from `related_table` inner join `pivot_table` on `related_table`.`id` = `pivot_table`.`related_id` 
    where `pivot_table`.`user_id` = `table`.`id` 
        and (`some_field` like ? or `some_field` like ?)) >= 1