4

I have no qlue why I can't get the following to work:

DB::table('twitter_hashtags')->paginate(5);

Every single time I get (the second number tends to differs)

Allowed memory size of 134217728 bytes exhausted (tried to allocate 95735352 bytes)

I tried using as in (18776710), but that did not make any difference

DB::connection()->disableQueryLog();

Removing ->paginate(5) does not make any difference.

When I try:

DB::select('SELECT * FROM twitter_hashtags');

It works fine, but then I can't use the build in the pagination option.

Anyone a suggestion?

The table twitter_hashtags has currently 5500 records. An id, tweet_id and the hashtag are saved, so it can't be the problem that the table is too big.

The table's size:

Data    384,0   KB
Index   464,0   KB
Total   848,0   KB

Update

As requested some more information

This is the action

public function getHashtags()
{           
    DB::connection()->disableQueryLog(); // With or without does not make a difference
    $retweets = DB::table('twitter_hashtags')->paginate(10);        

    // Show the page
    return View::make('twitter/retweets', compact('retweets'));

}

As you see, I use the view of retweets, the problem exits also in retweets, or nearly any other table I try to grab the data from this way.

The 'view'

</pre><? print_r($retweets) ?></pre>

The migration I used to create the table

public function up()
{
    Schema::create('twitter_hashtags', function($table)
    {
        // Basic run information
        $table->increments('id');
        $table->string('status_id')->index();
        $table->string('hashtag')->index();

        // Misc.
        $table->timestamps();
    });
}

These are the first 100 or so lines of the response when raised the memory limit to 256M

Illuminate\Database\Query\Builder Object
(
    [connection:protected] => Illuminate\Database\MySqlConnection Object
        (
            [pdo:protected] => PDO Object
                (
                )

            [queryGrammar:protected] => Illuminate\Database\Query\Grammars\MySqlGrammar Object
                (
                    [wrapper:protected] => `%s`
                    [selectComponents:protected] => Array
                        (
                            [0] => aggregate
                            [1] => columns
                            [2] => from
                            [3] => joins
                            [4] => wheres
                            [5] => groups
                            [6] => havings
                            [7] => orders
                            [8] => limit
                            [9] => offset
                            [10] => unions
                        )

                    [tablePrefix:protected] => 
                )

            [schemaGrammar:protected] => 
            [postProcessor:protected] => Illuminate\Database\Query\Processors\Processor Object
                (
                )

            [events:protected] => Illuminate\Events\Dispatcher Object
                (
                    [container:protected] => Illuminate\Foundation\Application Object
                        (
                            [booted:protected] => 1
                            [bootingCallbacks:protected] => Array
                                (
                                    [0] => Closure Object
                                        (
                                            [parameter] => Array
                                                (
                                                    [$app] => 
                                                )

                                        )

                                    [1] => Closure Object
                                        (
                                            [static] => Array
                                                (
                                                    [instance] => Illuminate\Log\LogServiceProvider Object
                                                        (
                                                            [defer:protected] => 1
                                                            [app:protected] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                        )

                                                )

                                        )

                                    [2] => Closure Object
                                        (
                                            [static] => Array
                                                (
                                                    [instance] => Illuminate\Mail\MailServiceProvider Object
                                                        (
                                                            [defer:protected] => 1
                                                            [app:protected] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                        )

                                                )

                                        )

                                    [3] => Closure Object
                                        (
                                            [static] => Array
                                                (
                                                    [instance] => Illuminate\Queue\QueueServiceProvider Object
                                                        (
                                                            [defer:protected] => 1
                                                            [app:protected] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                        )

                                                )

                                        )

                                    [4] => Closure Object
                                        (
                                            [static] => Array
                                                (
                                                    [instance] => Illuminate\Translation\TranslationServiceProvider Object
                                                        (
                                                            [defer:protected] => 1
                                                            [app:protected] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                        )

                                                )

                                        )

Update 2

As requested. This is the response:

Array
(
    [total] => 5689
    [per_page] => 5
    [current_page] => 1
    [last_page] => 1138
    [from] => 1
    [to] => 5
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1
                    [status_id] => 384992474579484672
                    [hashtag] => Twenterand
                    [created_at] => 2013-10-01 11:00:02
                    [updated_at] => 2013-10-01 11:00:02
                )

            [1] => stdClass Object
                (
                    [id] => 2
                    [status_id] => 384992323190280192
                    [hashtag] => Twenterand
                    [created_at] => 2013-10-01 11:00:03
                    [updated_at] => 2013-10-01 11:00:03
                )

            [2] => stdClass Object
                (
                    [id] => 3
                    [status_id] => 384989174014545921
                    [hashtag] => PVDA
                    [created_at] => 2013-10-01 11:00:03
                    [updated_at] => 2013-10-01 11:00:03
                )

            [3] => stdClass Object
                (
                    [id] => 4
                    [status_id] => 384988499188801536
                    [hashtag] => GR2014
                    [created_at] => 2013-10-01 11:00:03
                    [updated_at] => 2013-10-01 11:00:03
                )

            [4] => stdClass Object
                (
                    [id] => 5
                    [status_id] => 384986184092356608
                    [hashtag] => GR2014
                    [created_at] => 2013-10-01 11:00:03
                    [updated_at] => 2013-10-01 11:00:03
                )

        )

)
                                )

                                        )

Update 3

Here the code I use for the getStatuses

public function getStatuses()
{           
    // Get all the paginated statuses
    $statuses = DB::table('twitter_statuses')
            ->select('status_id', 'text', 'user_screen_name','datetime','place')
            ->orderBy('datetime', 'DESC')
            ->paginate(10);

    // Show the page
    return View::make('twitter/statuses', compact('statuses'));
}

And the complete view file

@extends('layouts/default')

{{-- Page title --}}
@section('title')
Twitter Statuses ::
@parent
@stop

{{-- Page content --}}
@section('content')
<h1>Twitter Statuses</h1>
<div class="container">
    <table class="table">
        <tr>
            <th>Datum</th>
            <th>Gebruiker</th>
            <th>Tweet</th>
            <th>Locatie</th>
        </tr>
    <?php foreach ($statuses as $status): ?>
        <tr>
            <td>{{ $status->datetime; }}</td>
            <td><?php echo $status->user_screen_name; ?></td>
            <td><?php echo $status->text; ?></td>
            <td><?php echo $status->place; ?></td>
        </tr>
    <?php endforeach; ?>
    </table>
    <?php echo $statuses->links(); ?>
</div>
@stop
Community
  • 1
  • 1
Martijn Thomas
  • 861
  • 3
  • 13
  • 24
  • trying to allocate 95megs at once? That's a pretty dang big "basic" query. – Marc B Oct 18 '13 at 15:41
  • Yes I know, but what is not basic on the query... it is textbook laravel coding, no joins, no wheres etc... – Martijn Thomas Oct 18 '13 at 15:43
  • Does it work when you try something like `findAll()` ? or with `->skip($offset)->take($page_size)` ? – Glad To Help Oct 18 '13 at 15:50
  • `findAll()` is not a recognized method using `get()` works to retrieve all rows. When using `DB::table('twitter_hashtags')->skip(0)->take(10);` (i guess thats the basis behind the pagination) results in the same memory error. – Martijn Thomas Oct 18 '13 at 16:25
  • How about increasing the memory_limit to 256 or higher? Because 5000 records is not that much data there should be no issue with that. Have you checked the generated SQL? If so what does it return? – pat Oct 18 '13 at 17:09
  • @PatrikStorm I just did, that results in a very strange response. It results in a 16M big page with a huge returned object... but not the intended data. I see nearly every part/namespace of the framework showing up... Is it me or does this look like a bug in laravel4? I can't imagine that no one else has had the same problem... – Martijn Thomas Oct 18 '13 at 17:28
  • Could you show more of your code, maybe that could help finding out the problem. – pat Oct 18 '13 at 18:27
  • @PatrikStorm i just added some more details, not sure what else I can provide you guys with, but if you need something let me know. – Martijn Thomas Oct 18 '13 at 18:42
  • Hmm, try `
    {{ print_r($retweets->toArray()) }}
    ` and let us know if you encounter the error or if there is any change
    – Glad To Help Oct 18 '13 at 19:54
  • @GladToHelp I added a second update. I do get a response now that makes more sense. But how do I get the paginator to work from here... Or more important why am I getting this problem. Or am I going to fast right now? – Martijn Thomas Oct 18 '13 at 20:13
  • The important thing here is whether do you encounter the memory error? Because if you don't, it means that the memory limit was hit because the Database Query objects that wrap your data are large very large and you were trying to output them – Glad To Help Oct 18 '13 at 20:15
  • 1
    @GladToHelp using toArray, I don't get the memory error, the response I get is the one under Update 2. – Martijn Thomas Oct 18 '13 at 20:34
  • Well the solution should be simple then - you just iterate through the collection in the view and only output your table attributes instead of making `print_r()`. Your data should already be paginated in the controller where you call the `paginate()` method. Let me know if this works for you. – Glad To Help Oct 18 '13 at 20:38
  • Shouldnt it be return View::make('twitter.retweets')->with(compact('retweets')); – pat Oct 19 '13 at 09:40
  • @PatrikStorm In Laravel you can choose between '/' and '.' syntax for views' paths, so both cases would work. Laravel is smart enough to determine that on its own. – Glad To Help Oct 19 '13 at 10:00
  • Ok, didnt know about that. How about the way the data is passed? Theres no with method? – pat Oct 19 '13 at 10:03
  • Scrap that, forgot you can pass data as an array of data as an second argument. I think the data has to be an array in this case, so maybe try with the with method? – pat Oct 19 '13 at 10:18
  • The wierd thing is, this whole action is preforming strange, the template does not inheret the parent layout... But more important why should I in this case use the `toArray()` option to get this working, but in an other action (getStatuses) everything works fine, and there the table is even bigger... I just don't get why this won't work as it should... Guys don't get me wrong I appreciate all the help and I'm not blaming you :) – Martijn Thomas Oct 19 '13 at 16:14
  • It's ok Thomas, np. As you might expect, without posting the code for `getStatuses` it is not possible for the members of the community to see what you are doing differently and answer your question specifically. If I were you, I would double check the code. – Glad To Help Oct 19 '13 at 20:33
  • I did expect that ;) @GladToHelp I added the code for `getStatuses`. I checked the code several times, rewrote it even more often (it is not much code..) and I no clue where my mistake or the problem might be. – Martijn Thomas Oct 19 '13 at 21:34
  • I started over again, and it seems to be working this time. I'll let you guys know if I run into any problems again. And many many thanks for now!!! – Martijn Thomas Oct 20 '13 at 11:54

1 Answers1

2

I just had the same issue. Solution is simple: Just add ->get(); at the end of the query:

public function getStatuses()
{           
    // Get all the paginated statuses
    $statuses = DB::table('twitter_statuses')
            ->select('status_id', 'text', 'user_screen_name','datetime','place')
            ->orderBy('datetime', 'DESC')
            ->paginate(10)
            ->get() 
;

    // Show the page
    return View::make('twitter/statuses', compact('statuses'));
}
Bogdan Lashkov
  • 319
  • 3
  • 17
  • Not in my case. I'm having this issue with my Eloquent querys since today. Was fine during 5 months, now without touching those codes it started to fail. Every query use `->get()`. Don't know what is happening. My memory_limit if far above the amount that is been tried to allocate, plus, I tested with a simple php file loading some csv of 50MB, setting the memory limit to 49MB, and I got the error, then I raise the memory limit to 51MB and it pass, but not with the Eloquent queries. – pmiranda May 15 '19 at 13:16