laravelvariablesforeachcontrollerlaravel-models

Invalid argument supplied for foreach() view returning null


When I created a variable in the Model named "statuses" and called it back in controller I got the array data. However when calling the second variable it's returning null and giving me error in the blade.

In Model

/**
 * Statuses.
 *
 * @var array
 */
protected $statuses = [
    0 => 'InActive',
    1 => 'Published',
    2 => 'Draft',
    3 => 'Scheduled',
];

/**
 * Visibilities.
 *
 * @var array
 */
protected $visibilities = [
    0 => 'Public',
    1 => 'Private',
    2 => 'Password Protected',
];

In the Controller

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    $article        = new Post();
    $categories     = Category::all();
    $tags           = Tag::all();
    $statuses       = $article->statuses;
    $visibilities   = $article->visibilities;

    return view('posts.create', compact('categories','tags','statuses', 'visibilities'));
}

When I dd($article) I get

dd of article

When I dd($statuses) I get

Statuses

When I dd($visibilities) it returns null while it there

And than I call it in blade

                        @foreach($visibilities as $key => $value)
                            <option value="{{ $key }}" {{ (isset($post->visibility) && $post->visibility == $key) ? 'selected' : '' }} >{{$value}}</option>
                        @endforeach

I get an error message

Invalid argument supplied for foreach() (View:

What did I do wrong to get null return for visibilities?

EDIT: I'm pasting my trait for relation

namespace App\Models\Traits\Attributes;

trait BlogAttributes
{
    /**
     * @return string
     */
    public function getActionButtonsAttribute()
    {
        return '<div class="btn-group" role="group" aria-label="'.trans('labels.backend.access.users.user_actions').'">'.
                $this->getEditButtonAttribute('edit-blog', 'admin.blogs.edit').
                $this->getDeleteButtonAttribute('delete-blog', 'admin.blogs.destroy').
                '</div>';
    }

    /**
     * Get Display Status Attribute.
     *
     * @var string
     */
    public function getDisplayStatusAttribute(): string
    {
        return $this->statuses[$this->status] ?? null;
    }

    /**
     * Get Statuses Attribute.
     *
     * @var string
     */
    public function getStatusesAttribute(): array
    {
        return $this->statuses;
    }

        /**
     * Get Display Visibility Attribute.
     *
     * @var string
     */
    public function getDisplayVisibilityAttribute(): string
    {
        return $this->visibilities[$this->visibility] ?? null;
    }

    /**
     * Get Visibility Attribute.
     *
     * @var string
     */
    public function getDisplayVisibilitiesAttribute(): array
    {
        return $this->visibilities;
    }
}

Solution

  • Thanks to @lagbox helped resolving the issue.

    It was changing getDisplayVisibilitiesAttribute() to getVisibilitiesAttribute()