1

Update Laravel 5.3 from 5.2 and after I am getting an error

https://i.stack.imgur.com/yEdH3.png

FatalErrorException in Builder.php line 638: Call to a member function all() on array

I used Laravel shift tool to upgrade framework and after successful composer update, I am facing this issue.

Code

public function index()
{
    $static_block_array = [];
    $static_block       = StaticBlock::whereIn('identifier', [
        'DESKTOP_STORE_FRONT_ROW_1_BLOCK',
        'DESKTOP_STORE_FRONT_ROW_2_BLOCK',
        'DESKTOP_BOTTOM_BLOCK','SOCIAL_MEDIA_ICON_BLOCK','TOP_ROW_HOMEPAGE_BLOCK'])
        ->remember(cacheTimeOut(CATALOG_CACHE_TIMEOUT))
        ->with("staticBlockContent")
        ->cacheTags(TAG_CATALOG)
        ->whereStatus(1)
        ->get();

    foreach ($static_block as $value) {
        $static_block_array[$value->identifier] = isset($value->staticBlockContent[0]) ? $value->staticBlockContent[0]->content : "";
    }

    return View::make('home/index')
            ->with('desktop_store_front_first_row', array_get($static_block_array, 'DESKTOP_STORE_FRONT_ROW_1_BLOCK', ''))
            ->with('desktop_store_front_second_row', array_get($static_block_array, 'DESKTOP_STORE_FRONT_ROW_2_BLOCK', ''))
            ->with('desktop_top_row_content', array_get($static_block_array, 'TOP_ROW_HOMEPAGE_BLOCK', ''))
            ->with('desktop_bottom_block', array_get($static_block_array, 'DESKTOP_BOTTOM_BLOCK', ''));
}
Jigar
  • 3,055
  • 1
  • 32
  • 51
  • can you post the offending snippets? – Bagus Tesa May 05 '17 at 08:02
  • @BagusTesa added please check now – Jigar May 05 '17 at 08:06
  • @BagusTesa the error I am getting it show error on vendor directory, vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php line 638: – Jigar May 05 '17 at 08:08
  • the one that calls that `->all()`, where does it being called? in your code or rather, in your controller for that problematic page. – Bagus Tesa May 05 '17 at 08:09
  • @BagusTesa it's on framework function in vendor directory which is created by laravel framework – Jigar May 05 '17 at 08:12
  • @BagusTesa after an upgrade I am getting this error before its working proper. – Jigar May 05 '17 at 08:13
  • @jigarhalani: Are you using MongoDB? – Ravi Hirani May 05 '17 at 08:19
  • @RaviHirani No. I am using Mysql – Jigar May 05 '17 at 08:22
  • i know @jigarhalani, i ever experience such thing. i want you to change the `->all()` into [`->get()`](https://laravel.com/api/5.3/Illuminate/Database/Eloquent/Builder.html#method_get). but we can't be certain unless reading the code. since L4, we have no `->all()` in [Eloquent Query Builder](https://laravel.com/api/4.2/Illuminate/Database/Eloquent/Builder.html). Perhaps it was previously using Query Bulder? but that class also did not have `->all()`... or perhaps i miss something. – Bagus Tesa May 05 '17 at 08:52
  • i can remember now.. `->all()` does exists in [Eloquent Collection](https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Collection.html#method_all). – Bagus Tesa May 05 '17 at 08:55
  • @BagusTesa https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0 please search all() here – Jigar May 05 '17 at 09:00
  • @BagusTesa I can not change this because it's in vendor directory and we can't change vendor directory code – Jigar May 05 '17 at 09:01
  • no, not changing the vendor directory, but the code that calls into the vendor, your code. the one that handle that erred page. not to mention, notice the `->get()` in `$users = DB::table('users')->get()->all();` the link that you provided. Since `->get()` returns collection, then `->all()` returns the array. **I believe** your code calls `->all()`directly from Query Builder instance, something like `DB::table('users')->all()`. – Bagus Tesa May 05 '17 at 09:03
  • @BagusTesa error comes because of remember() function if I removed that then it's working correctly. – Jigar May 05 '17 at 09:30
  • hmm, im intrigued with that `remember()` function.. where does it located..? – Bagus Tesa May 05 '17 at 10:21
  • it's used to cache query in redis server – Jigar May 05 '17 at 11:53

1 Answers1

4

Issue is solved now. we created a custom logic to cache query in remember method we return a plain array but after converting to a collection object issue is solved.

 return collect($cache->remember($key, $minutes, $callback));
Jigar
  • 3,055
  • 1
  • 32
  • 51