0

I have about 2000 records in my database and i want to fetch them using view into my datatable.

When i run my code, i noticed my database is only showing 166 records. Why is that happening ?

Controller

 $chunks =  Items::
        where('is_active', 1)
        ->pluck('name','code','barcode','is_active')
        ->chunk(10);
        foreach ($chunks as $key => $chunk) {
            $chunks[$key] = array_values($chunk->toArray());
        }

     Log::info($chunks);
    return $chunks;
5
  • 1
    Can it be that some records have the field is_active != 1? Commented Sep 20, 2021 at 17:06
  • No, i have checked my database and every product is set to 1 for is_active column @Tiago Commented Sep 20, 2021 at 17:13
  • 1
    I implemented code like what you wrote, and it returning 2000 records, Please check your database is it really have 2000 items ? and does all of them is_active == 1, you could test: Items::where('is_active', 1)->get() Commented Sep 20, 2021 at 17:32
  • I have tested several times @RatebHabbab, i have 2000 records. I think the issue may be from the pluck Commented Sep 20, 2021 at 17:36
  • What version of Laravel are you using? I can't get it to work under 8. Pluck accepts at most 2 values: column and key. You're passing in 4 Commented Sep 20, 2021 at 17:49

1 Answer 1

2

Your problem is related to the way you retrieve your data:

 $chunks = Items::
        where('is_active', 1)
        ->pluck('name','code','barcode','is_active');

Here, you are misusing pluck(). This method returns an array (a Collection in this case but it's the same here) where the first argument is the value and the second argument is the key.

You are passing 4 parameters, the last two are useless.

Here is what's happening:

 $chunks = Items::
        where('is_active', 1)
        ->pluck('name','code'); //removed the last two parameters, they are not used

$chunks is a Collection, where the key is the code and the value is the name.

A Collection, like an array, cannot have the same key multiple times. Then, if two users share the same code, the second user will "overwrite" the first one. The third will overwrite the second and so on.

At the end, you'll have a Collection where the number of items will be equal to the number of unique code that are active (where('is_active', 1).

This why it fails.

How to fix this? Use get(), it does exactly what you expected pluck to do:

$chunks =  Items::
        where('is_active', 1)
        ->get(['name','code','barcode','is_active']) //be careful, array here
        ->chunk(10);
        foreach ($chunks as $key => $chunk) {
            $chunks[$key] = array_values($chunk->toArray());
        }

     Log::info($chunks);
    return $chunks;
Sign up to request clarification or add additional context in comments.

Comments

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.