Cowgirl
Cowgirl

Reputation: 1494

Laravel QUERY does not result anything even if there is no condition

My query was working fine till yesterday, but today, my query yields empty results. I tried even with no conditions like this

"select `person`.`name`, `path`, `person`.`username`, `price`, `service`, `person`.`city`, `person`.`streetaddress`, `person`.`postalcode`, `date` from `person` inner join `services` on `person`.`id` = `services`.`person_id` inner join `imagesforperson` on `person`.`id` = `imagesforperson`.`person_id` group by `person`.`name

This is my query when I try to debug it with toSql(). As you can see, I have not supplied any WHERE condition, but it still returns empty collection.

Collection {#212 ▼
  #items: []
}

Can someone advice what could have gone wrong? Any tips to debug this issue?

I am using Larvel 5.4, using MySQL as database.

Code:

$person = \App\person::query()
        -> select (['person.name', 'path', 'person.username', 'price', 'service','person.city', 'person.streetaddress', 'person.postalcode', 'date'])
        -> groupBy('person.name')
        -> join ('services' , 'person.id', '=', 'services.person_id')
        -> join ('imagesforperson' , 'person.id', '=', 'imagesforperson.person_id')
        -> get();

dd($person) //to debug, this returns empty collection as posted above

Upvotes: 0

Views: 171

Answers (1)

JasonJensenDev
JasonJensenDev

Reputation: 2407

It looks like you have two inner joins on your query, which means that if those related records don't exist (like services and imagesforperson), you won't get any results. You might think about starting there. Maybe you could try removing the joins and/or making them left join or something so that your query would still return a person record even if no services or imagesforperson existed.

Upvotes: 3

Related Questions