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

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

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();
    dd($shop->toArray());

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

Solution:

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.

Another must read:  Dynamically Add Input Fields And Submit To Database With jQuery and PHP

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

Example

Also, you can fix the bug yourself.

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

pluck() on array

<?php

$shops_ids = [
  [
    159739600000,
    39103.5828125
  ],
  [
    159739000000,
    39102.619270833
  ]
];

$data_result = [];

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

print_r($data_result);

output

Array
(
    [0] => 23:06
    [1] => 00:26
)

Web Programming Tutorials Example with Demo

Read :

Summary

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

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 infinityknow.com 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.

Leave a Reply

Your email address will not be published. Required fields are marked *