3

Inside a partial for creating and editing form I had this to decide whether to display the current value (when editing) or an old value (when editing or creating and not passing the validator)

<input type="text" name="title" value="{{ old('title')?old('title'):$model->title }}">

This became a pain, so I created a custom blade function in app/helpers.php

function decide($inputName, $model){
    return old($inputName)?old($inputName):$model->$inputName;
}

This works well however, what I want to achieve is ultimately this

<input type="text" name="title" value="{{ decide('title') }}">

Without the need to pass in $model

2 Answers 2

2

You can use Extending Blade, which allows you to create your own custom blade function.

Read documentation here

Sign up to request clarification or add additional context in comments.

Comments

2

You can pass a default to old.

old('title', $model->title)

A null old value for title will use the default, no need for the ternary.

I don't know how you'd make the association without the model, however.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.