How to Solve Error call to a member function toArray() on null?

Rate this post

Question: What It Is & How to Fix php error Like call to a member function toArray() on null, fails() on array, pluck() on array, getrelationexistencequery() on array, getapipath on array, all() on string and store() on array

PHP Error :call to a member function toArray() on null (What It Is & How to Fix It)

Best Solution for you intermittently receive the PHP or Laravel error: “Call to a member function toArray() on null”. like as a toarray null pointer exception

Also you can read my prev Tutorials for find top 10 php programmers

Read Also:  php undefined index - How To Remove Undefined Index Error in PHP Variables?

Problem: call to a member function toArray() on null

This error comes when the PHP execution script doesn’t allow us to run scripts. I also found the same error when tried to run a PHP or Laravel script.

    $shop = Shop::with('profile')->whereUsername($shop_name)->firstOrFail();
    $products = $shop->products->all();

php error reporting, php error log, php error, php error_log, php error handling, php turn on error reporting, php error checker, php fatal error allowed memory size of, undefined index php error, php error 500, php fatal error: allowed memory size of


I think this happens because $products is a collection of objects not an object like $shop. Check querying-relations in the Laravel main documentation the red area in the end of that section. That explains why $products[0]->toArray() works fine.


Also, you can fix the bug yourself.

$shop = \App\Shop::where('name','pakainfo')->first();
	$shop = $shop->toArray();
retrun $shop;

pluck() on array


$shops_ids = [

$data_result = [];

foreach($shops_ids as $id_shop) {
  $data_result[] = date('H:i', $id_shop[0]);



    [0] => 23:06
    [1] => 00:26

Web Programming Tutorials Example with Demo

Read :

Read Also:  Enable PHP Error Log reporting Display on Web Browser


You can also read about AngularJS, ASP.NET, VueJs, PHP.

Free Live Chat for Any Issue

I hope you get an idea about php error reporting => first()->toArray() fails with “Call to a member function toArray() on null”.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.