N
N
Northern Lights2019-10-25 15:23:58
Laravel
Northern Lights, 2019-10-25 15:23:58

The toArray method only returns the first level of nesting?

What am I doing wrong?
In the model:

class DocumentType extends Model
{
    protected $fillable = [
        'id',
        'code',
        'name',
        'active',
    ];

    public $childs;

Calling:
print_r(DocumentType::getTree());
Result:
Illuminate\Database\Eloquent\Collection Object
(
    [items:protected] => Array
        (
            [0] => App\Models\Docflow\DocumentType Object
                (
                    [fillable:protected] => Array
                        (
                            [0] => id
                            [1] => code
                            [2] => name
                            [3] => active
                        )

                    [childs] => Illuminate\Support\Collection Object
                        (
                            [items:protected] => Array
                                (
                                    [7] => App\Models\Docflow\DocumentType Object  .............

If so:
print_r(DocumentType::foo()->toArray());
return result:
Array
(
    [0] => Array
        (
            [id] => 6
            [parent_id] => 2
            [code] => 
            [name] => Приказ на переводе на другую должность
            [active] => 1
        )

Why is the childs property not present in the resulting array and why is it not converted to an array?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2019-10-25
@php666

Because what is serialized in $this->attributes and in $this->relations * . About the fact that you added some property to the class, Laravel does not know anything and whether you want to serialize it or not - too. In addition to the source code, it is also useful to read the documentation .
PS children, not childs.
* in fact, the logic is more complicated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question