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.

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


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 Comment