Using laravel presenters inside lighting / html forms

I am using laravel presenters from laracasts video. In principle, this is not so important. I want to format the output when I use Form::model

. So, for example, I have a phone number:

<div class="form-group">
   {!! Form::label('phone', 'Phone' ) !!}
   {!! Form::text('phone', null, ['class' => 'form-control', 'id' => 'phone']) !!}
</div>

      

In the DB, I store phones in e.164 format, but when I output them I want to display them in a more readable format. But data receivers from Laravel won't help me because in my application I want to use e.164. If I set an accessory for the phone attribute, I cannot get e.164 inside my classes. So, instead of accessories, I'm speaking to users, but that doesn't really matter. Let's say I want to use php function number_format

while outputting model attributes inside Form::text

. How can i do this?

+3


source to share


1 answer


There's nothing forcing you to have a one-to-one accessory to field relationships. This way, you can create a formatted phone attribute for use in the form and use the phone attribute directly from your classes.

public function getFormattedPhoneAttribute()
{
    return // formatted $this->phone
}

      

If you already have creators, you can use them in your form instead of using the :: model form.



<div class="form-group">
   {!! Form::label('phone', 'Phone' ) !!}
   {!! Form::text('phone', $user->present()->phone, ['class' => 'form-control', 'id' => 'phone']) !!}
</div>

      

Either should work for you, but I would recommend that you pick one: Accessory or Speaker, not both.

+1


source







All Articles