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

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

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.

Read Also:  sample sql database for practice

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.

Read Also:  JavaScript Capitalize first letter uppercase, lowercase and camel case

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Dynamically create table rows and Columns in php
  2. Read Also:  php try catch Block Exception Handling
  3. Read Also:  Image Upload in Laravel 7 Example Tutorial
  4. Read Also:  Increase WordPress Memory Limit using PHP
  5. Read Also:  check array empty php
  6. Read Also:  how to use between in sql
  7. Read Also:  PHP Laravel 6 DataTables Tutorial
  8. Read Also:  Facebook Api Get User Id Profile Information using PHP
  9. Read Also:  Laravel 6 orderBy for Multiple columns
  10. Read Also:  jQuery Ajax Get JSON in Laravel Example
CLOSEX