1

I'm building a form with LaravelCollective and I'm taking the benefits from Form Model Binding to use it creating or editing the model.

I have this field:

{{ Form::text('price', null, ['class' => 'form-control']) }}

If I open the form with Form::model() it shows the model value. The problem is that I need it to have a default value of 0.

I achieved it this way but I'm looking for a more elegant way

{{ Form::text('price', isset($product)? $product->price : 0, ['class' => 'form-control']) }}

If I make this:

{{ Form::text('price', 0, ['class' => 'form-control']) }}

It always override the value to 0 even if I'm editing a Model.

Alan
  • 2,559
  • 4
  • 32
  • 53
  • If you're using php 7, you can do `$product->price ?? null` - but otherwise, I don't think there's an easier solution. – Joel Hinz Aug 25 '16 at 19:06

3 Answers3

1

You can try Laravel form model binding.Reference Link

Routes:

Route::get('/user/edit/{id}', [
            'as' => 'updateUser',
            'uses' => 'UserController@getUserEditFrom'
        ]);

Controller:

public function getUserEditFrom($id, UserRepository $userRepository)
    {
        return view('users.edit', [
            'user' => $userRepository->find($id)
        ]);
    }

View

{!! Form::model($user, ['url' => route('updateUser', ['id' => $user->id])]) !!}
        <div class="form-group {{ $errors->first('name', 'has-error') }}">
                {!! Form::label('name', 'Name *') !!}
                {!! Form::text('name', null, ['class' => 'form-control']) !!}
                {!! $errors->first('name', '<span class="help-block">:message</span>') !!}
            </div>
             <div class="form-group {{ $errors->first('username', 'has-error') }}">
                {!! Form::label('username', 'Username *') !!}
                {!! Form::text('username', null, ['class' => 'form-control']) !!}
                {!! $errors->first('username', '<span class="help-block">:message</span>') !!}
            </div>
             <div class="form-group {{ $errors->first('email', 'has-error') }}">
                {!! Form::label('email', 'Email *') !!}
                {!! Form::text('email', null, ['class' => 'form-control']) !!}
                {!! $errors->first('email', '<span class="help-block">:message</span>') !!}
            </div>

    {!! Form::close() !!}
Parithiban
  • 1,656
  • 11
  • 16
0

you can change the second argument to $product->price || 0

or:

it seems you have a product model with a price attr that is nullable. Although this does not directly answer your question, but I suggest you set a default value for every possible column, so such problems and much more don't get in the way. you can simply set the default value of price column to zero, or add an event listener for model::creating, check for null values and change them to 0.

Fatemeh Majd
  • 719
  • 8
  • 22
0

You can achieve by this way also {{ Form::text('price', @$product->price ? :0, ['class' => 'form-control']) }}

Abhinav Gupta
  • 511
  • 4
  • 12