skip to Main Content

I’m trying to retrieve some data from my MySQL database, but It’s not working using Eloquent (I’m getting 0 rows). I’s a very simple query, but is driving me crazy.

This is the query in PHP:

$slideshowIdList = Media_slideshow::select('idSlideshow')
                                    ->whereIn('idMedia',$idList)
                                    ->groupBy('idSlideshow')
                                    ->get()
                                    ->toArray();

Laravel’s query log shows that the query I wanted to do is okay:

Array
(
    [0] => Array
        (
            [query] => select `idSlideshow` from `media_slideshow` where `idMedia` in (?) group by `idSlideshow`
            [bindings] => Array
                (
                    [0] => 11
                )

            [time] => 0.49
        )

)

And this it the same query showing the results in Phpmyadmin:

enter image description here

The Media_slideshow model:

class Media_slideshow extends Model
{

    private $idMedia;
    private $idSlideshow;
    protected $table = "media_slideshow";


}

Am I missing something?

UPDATE:
Hardcoding the second parameter in ->whereIn() works. Any clue?

->whereIn('idMedia',[11])

2

Answers


  1. Try this:

    $slideshowIdList = Media_slideshow::whereIn('idMedia', $idList)
                                    ->groupBy('idSlideshow')
                                    ->value('idSlideshow');
    $slideshowIdList = $slideshowIdList->toArray();
    
    Login or Signup to reply.
  2. The format of your $idList array is wrong. Try to print it as suggested. Refer to my comment above.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search