user3732216
user3732216

Reputation: 1589

Laravel Presenters

I'm curious to know if I need the null at the end of the presenter return statement since old() will return null if a value isn't found.

Event Form

<input type="date" data-plugin="datetimepicker" class="form-control" id="date" name="date" value="{{ old('date') ?? $event->formatted_form_date }}"/>

Presenter

public function formattedFormDate()
{
    return $this->model->date ? $this->model->date->format('m/d/Y') : null;
}

Upvotes: 0

Views: 421

Answers (1)

Norris Oduro
Norris Oduro

Reputation: 1039

Obviously value="{{ old('date') ?? $event->formatted_form_date }}" means If old('date') has a value, return that value else return $event->formatted_form_date.

Returning null from formattedFormDate() depends on the date attribute on the model. If its nullable, then it means there may be instances where $this->model->date would return null and hence the need for the return null else if its not nullable it is always guaranteed that the $this->model->date would never be null which makes the return null from the formattedFormDate() useless.

Upvotes: 1

Related Questions