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!
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 sign in or create an account to participate in this conversation.