Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

COACHTHEM's avatar

Call to a member function toArray() on null in laravel 5.5

I am getting this error "Call to a member function toArray() on null" while calling toArray() method on a model class

I am trying to fetch records for a model. There are cases where I might not get result from the model. In this situation if I bind toArray() \App\User::where([['name','Manny']])->first()->toArray(); it throws above error.

This happen when I bind first() method with toArray().

0 likes
15 replies
RamjithAp's avatar

Try this

$user = \App\User::where('name','Manny')->first();
if($user){
$user = $user->toArray();
}
retrun $user;
5 likes
COACHTHEM's avatar

Hi @RamjithAp , I am looking for methods that would return empty single dimensional array when there is no record found in the database. In this case I will have to put a check every time to bind toArray() method

with ->get()->toArray() it works fine the way I expect. With get() if I bind toArray() it returns an empty array() and no error is thrown.

COACHTHEM's avatar

@tisuchi with below where method you can specify multiple clauses

    $user = \App\User::where([
        ['name', 'Manny'],
        ['id', 100]                                  //Second where clause
    ])
    ->first()
    ->toArray();

My problem is not with the where clause. I am trying to fetch one record form DB as a single dimensional array, for which I am using ->first()->toArray() method. The problem is that when there is no record in the DB it throws the above error. I expect it to be empty array. I am trying to find out if there is any other way to achieve this.

tisuchi's avatar

I see..

You know why you are getting this error? First of all, you are accessing first record from Database, and than based on that record, you are converting to array.

Now, since there is no record, it just return an empty object. And now you are trying to convert to array an empty object. That's why you are getting error. In this case, definitely you need to check whether the object is empty or not before convert to array.

1 like
bobbybouwmann's avatar

The first method can return null when no record is found. You should check for that

$user = \App\User::where('something', 'value')->first();

if ($user) {
    $result = $user->toArray();
}

$result = [];
1 like
kamleshcgtechno's avatar

I think you should define $result = []; before if condition like:

$user = \App\User::where('something', 'value')->first();

$result = []; if ($user) { $result = $user->toArray(); }

echo '

';
print_r($result);

kamleshcgtechno's avatar

I have added "echo '< pre >'; " tag in my reply, but I think Laracasts forum does not support html tags. Thanks

kamleshcgtechno's avatar

Laracasts Text editor is third class. Basic next line feature is also not working properly. Very disappointed :(

Snapey's avatar

why bother even replying to 3 year old thread?

These days we can rely on everyone having the null coalesce operator

\App\User::where([['name','Manny']])->first() ?? [];
COACHTHEM's avatar

Hi @bobbybouwmann, when using ->first() every time I will have to put a check to convert it to an array which I want to avoid.

I am expecting a result just the way ->get() returns but single dimensional array. here even if there is no record in the DB it return an empty array. I dont have to put a check to use toArray() method.

below code does not throw error if I use toArray() without a check

    $user = \App\User::where([
        ['name','manny']
    ])
    ->get()           //WITH get() WORKS FINE AND DONT HAVE TO PUT CHECK TO USE toArray()
    ->toArray();

where as below code throws error without a check

    $user = \App\User::where([
        ['name','manny']
    ])
    ->first()         //WITH first() THROWS ERROR, HAVE TO PUT CHECK TO USE toArray()
    ->toArray();
Snapey's avatar

Its not the same scenario though. Get is expecting to return a collection so you end up with an empty collection whereas first() is expecting to return a single model and null IS the most appropriate response.

What do you get with

    $user = collect(
        \App\User::where('name','manny')->first()
            )->toArray();
1 like
Snapey's avatar

Other than wrapping in collect() I think it has to be two-step otherwise the query would have to run twice;

    $q = User::where('name','manny')->first();

    $user = $q ? $q->toArray() : array();

2 likes
cmdobueno's avatar

You could simply create your own custom base model that all your models extend.

Within this base model, you could then create a function call firstArray() and wrap this check within to either return you an array of the single model or an empty array.

It has more re-usability.

2 likes

Please or to participate in this conversation.