1

I try do a model binding in edit.blade.php however, fieldname in database host_firstname is different from fieldname of Form host-firstname So, the data from database not show in the field when we run. (and I don't want to change data field-name in my view too)

I'm very new to Laravel and I'm not sure how to set default keyname in model to make it works. Does anybody has any solutions for this?

Database Schemas

  family_general_id int(10) unsigned NOT NULL AUTO_INCREMENT,

  host_firstname varchar(255) COLLATE utf8_unicode_ci NOT NULL,

  host_lastname varchar(255) COLLATE utf8_unicode_ci NOT NULL,

edit.blade.php

 {!! Form::model($survey, ['method' => 'PATCH', 'url' => 'surveys/' . $survey->family_general_id]) !!}
    <!-- Host-firstname Form Input-->
    <div class="form-group">
        {!! Form::label('host-firstname', 'Firstname:') !!}
        {!! Form::text('host-firstname', null, ['class' => 'form-control']) !!}
    </div>

    <!-- Host-lastname Form Input-->
    <div class="form-group">
        {!! Form::label('host-lastname', 'Lastname:') !!}
        {!! Form::text('host-lastname', null, ['class' => 'form-control']) !!}
    </div>
    {!! Form::close() !!}
2
  • As far as I know, you can't directly do it, I'd rather change the name in the view to match the one of the model. Commented Mar 12, 2015 at 8:56
  • Just interesting: why you cannot use underscored names? Commented Mar 12, 2015 at 10:49

1 Answer 1

1

Am not sure if this is possible at the moment. I took a look at Illuminate/Html/FormBuilder as you can see transformKey() only works with under_scores

/**
 * Get the model value that should be assigned to the field.
 *
 * @param  string  $name
 * @return string
 */
protected function getModelValueAttribute($name)
{
    if (is_object($this->model))
    {
        return object_get($this->model, $this->transformKey($name));
    }
    elseif (is_array($this->model))
    {
        return array_get($this->model, $this->transformKey($name));
    }
}

    protected function transformKey($key)
        {
            return str_replace(array('.', '[]', '[', ']'), array('_', '', '.', ''), $key);
        }

The solution i can suggest to use basic laravel form builder instead since this is a special case:

{!! Form::open(['method' => 'PATCH', 'url' => 'surveys/' . $survey->family_general_id]) !!}
    <!-- Host-firstname Form Input-->
    <div class="form-group">
        {!! Form::label('host-firstname', 'Firstname:') !!}
        {!! Form::text('host-firstname', null, ['class' => 'form-control']) !!}
    </div>

    <!-- Host-lastname Form Input-->
    <div class="form-group">
        {!! Form::label('host-lastname', 'Lastname:') !!}
        {!! Form::text('host-lastname', null, ['class' => 'form-control']) !!}
    </div>
    {!! Form::close() !!}

Then do something like:

class SurveyController extends Controller
{
  //after validation
  $host_firstname = Input::get('host-firstname');

  //send this to your database $host_firstname
  $survey = new Survey;

  $survey->host_firstname = $host_firstname;

  $survey->save(); 
}
Sign up to request clarification or add additional context in comments.

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.