0

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;
    }
}
David Buik
  • 522
  • 1
  • 8
  • 31
  • when you dd $article->visibilities in the controller what do you get? – Mátyás Grőger Feb 04 '22 at 14:29
  • The same result "null" – David Buik Feb 04 '22 at 14:30
  • not sure maybe the annotation ->visibilities reserved for something try to rename it and see if it is works. For example $avaibleVisibilities – Mátyás Grőger Feb 04 '22 at 14:32
  • Exactly I thought of that and I did already but no luck still "null" – David Buik Feb 04 '22 at 14:33
  • you should be getting `null` for both since they are protected properties (not visible from outside the class) which would cause `__get` to be called which would try to return an attribute/relationship or `null` ... did you create an accessor for the `statuses`? – lagbox Feb 04 '22 at 14:34
  • If you are mentioning the Trait I've edited the question with the code added – David Buik Feb 04 '22 at 14:38
  • you have an accessor for the `statuses` attribute which returns that array, that is why you get the array ... you don't have one for `visibilities` – lagbox Feb 04 '22 at 14:39
  • But I created an accessor for the visibilities as well public function `getDisplayVisibilitiesAttribute(): array` and `public function getDisplayVisibilityAttribute(): string` – David Buik Feb 04 '22 at 14:47
  • no, that would be for the attribute `displayVisibility` which is not `visibilities` – lagbox Feb 04 '22 at 14:48
  • Excuse me can you elaborate more, and do me a favor pasting the solution as an answer. Thank you – David Buik Feb 04 '22 at 14:49
  • `displayVisibilities != visibilities` .... `get{AttributeName}Attribute` is the format for accessors ... the only accessors related to 'visibilitiy' you have defined are for the attributes `displayVisibility` and `displayVisibilities`, so you would have to try accessing the property `displayVisibilities` to use the last one, `$post->displayVisibilities` – lagbox Feb 04 '22 at 14:50
  • Ok got the idea thanks for the information – David Buik Feb 04 '22 at 14:56

1 Answers1

0

Thanks to @lagbox helped resolving the issue.

It was changing getDisplayVisibilitiesAttribute() to getVisibilitiesAttribute()

David Buik
  • 522
  • 1
  • 8
  • 31