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

ElpsySec's avatar

How to get a model instance's attribute names?

So I am passing a model to a view. Let's say it's an Article model.

$article = Article::find(1);

return view('article.edit')->with('article', $article);

I then want to loop through the $article, check if that attribute's value is not null, and then put the value in an a text field. Something like:

@foreach($article->getAttributes() as $attribute)

{!! Form::text("$attribute", $article->$attribute) !!}

@endforeach

Any suggestions on how to do this? Thanks!

0 likes
1 reply
Kryptonit3's avatar

Not tested, but I would try

@foreach($article->getAttributes() as $attribute => $value)
    @if($value) // $attribute value is set - may also want to try @if($value !== null)
        {!! Form::text($attribute, $value) !!}
    @endif
@endforeach

Please or to participate in this conversation.