Paco Pinazo Guna
Paco Pinazo Guna

Reputation: 117

LARAVEL Getting 2 of the same columns

I have a table that has animals and another that has the images of the animals (id_image, id_animal).

The problem is that when I do this:

$animales = DB::table('animals')
->orderBy('date_found')
->join('images','images.id_animal','animals.id')
->limit(6)
->get();

When I do that it returns the same animal for each image that has. That is a problem becuase when I display all the animals it shows the same one multiple times Example

What can I do? I tried to use group but it doesn't work.

Thanks!

 [1]=>
    object(stdClass)#348 (15) {
      ["id"]=>
      int(5)
      ["id_user"]=>
      NULL
      ["race"]=>
      string(7) "Mestizo"
      ["species"]=>
      string(6) "Canina"
      ["gender"]=>
      string(5) "Macho"
      ["date_of_birth"]=>
      string(10) "2017-01-19"
      ["description"]=>
      string(54) "Un perrito muy bonito y pequeño que busca una familia"
      ["health"]=>
      string(8) "Muy bien"
      ["nickname"]=>
      string(6) "Tudels"
      ["place_found"]=>
      string(8) "Valencia"
      ["size"]=>
      string(7) "Mediano"
      ["date_found"]=>
      string(10) "2017-03-01"
      ["condition"]=>
      NULL
      ["id_animal"]=>
      int(1)
      ["url"]=>
      string(18) "/img/animals/7.jpg"
    }
    [2]=>
    object(stdClass)#347 (15) {
      ["id"]=>
      int(1)
      ["id_user"]=>
      NULL
      ["race"]=>
      string(7) "Mestizo"
      ["species"]=>
      string(6) "Canina"
      ["gender"]=>
      string(5) "Macho"
      ["date_of_birth"]=>
      string(10) "2017-01-19"
      ["description"]=>
      string(54) "Un perrito muy bonito y pequeño que busca una familia"
      ["health"]=>
      string(8) "Muy bien"
      ["nickname"]=>
      string(6) "Tudels"
      ["place_found"]=>
      string(8) "Valencia"
      ["size"]=>
      string(7) "Mediano"
      ["date_found"]=>
      string(10) "2017-03-01"
      ["condition"]=>
      NULL
      ["id_animal"]=>
      int(1)
      ["url"]=>
      string(19) "/img/animals/1.jpeg"
    }

Upvotes: 1

Views: 49

Answers (2)

TsaiKoga
TsaiKoga

Reputation: 13394

Because one animals has many images. So when you use join or leftjoin, and if there is one dog has two images, it will become two records which has same dog's columns with different images.

So you can use group by like this:

$animales = DB::table('animals')
              ->orderBy('date_found')
              ->join('images', 'images.id_animal', 'animals.id')
              ->groupBy('animals.id')
              ->limit(6)
              ->get();

And if you have set the strict mode in config/database.php, you need to enable the ONLY_FULL_GROUP_BY mode. You can check this How to write Laravel GroupBy Query?.

Upvotes: 1

Hassan Nasir
Hassan Nasir

Reputation: 146

Use left join here

$animales = DB::table('animals')
->orderBy('date_found')
->leftJoin('images','images.id_animal','animals.id')
->limit(6)
->get();

Upvotes: 2

Related Questions