an.leclerc's avatar

Problem with foreach - array offset on null

Hi,

I've a problem, in my component I've 2 methods :

#[Computed]
    public function resourceTypes()
    {
        return ResourceTypeModel::orderBy('name', 'ASC')->get();
    }

    #[Computed]
    public function periodicityTypes()
    {
        return PeriodicityTypeModel::orderByRaw("FIELD(reference, 'mensuelle', 'trimestrielle', 'semestrielle', 'annuelle') DESC")->get();
    }

In my blade template, I try to use foreach, it doesn't work

<flux:select variant="listbox" placeholder="Choose industry...">
	@foreach($this->resourceTypes as $item)
		<flux:select.option>{{ $item->name }}</flux:select.option>
	@endforeach
</flux:select>

I've this error

Trying to access array offset on null (View: /var/www/vendor/laravel/framework/src/Illuminate/Foundation/resources/exceptions/renderer/markdown.blade.php)

If I change resourceTypes by resources in my blade template IT WORKS, I don't what's wrong with resourceTypes !!! If anybody can help ?

0 likes
8 replies
jlrdw's avatar

Is the data there if you try a dd?

valentin_vranic's avatar

Do you have a public property defined with the name resourceTypes above int he code? Ensure that only the #[Computed] is present, thus the public property will override it.

Glukinho's avatar

@an.leclerc wrote he found the solution by himself answering my comment which I deleted later. I didn't expect deleted comment's replies are deleted too.

Glukinho's avatar

This is where safe delete concept fits well.

Glukinho's avatar

I like when deleted comment is seen as [ this comment was deleted ] text while it's descendants remain visible (you can see it on Reddit). This way no info is lost.

It implies comments are not really deleted from DB and no cascade on delete is applied. Just deleted_at timestamp is set. Activities / notifications behaviour should be tuned accordingly, it shouldn't be hard.

PS what is "OOT"?

an.leclerc's avatar

The problem was I had another parameter which have the same name. It made a conflict.

Please or to participate in this conversation.