Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theUnforgiven's avatar

Using form model binding

I'm using form model binding to edit a form, but I have two tables I need to get data for.

One table has username, password, status the other table has

fullname, address, email, phone etc

So how can I use

 {{ Form::model($user, ['method' => 'PUT', 'route' => ['profile.update', $user->id], 'class' => 'form-horizontal']) }}

To get all data from the two tables?

0 likes
43 replies
firstpointdev's avatar

could try linking the two with a relationship and then you only need to reference the one model?

theUnforgiven's avatar

I already have the relationship setup with two models, so I should be able to do $user->org_table->fullname right?

theUnforgiven's avatar

So in my form I would i get the value from the database from the other table.

 <div class="form-group">
         <div class="col-sm-2">Full Name</div>
             <div class="col-lg-5">
                 {{ Form::text('fullname', Input::old('fullname'), array('class'=>'form-control', 'disabled')) }}
             </div>
    </div>

This "fullname" is in a userInfo table not the user table.

I have two models which have the following in: User.php

   public function userInfo()
   {
    return $this->hasOne('UserInfo', 'user_id');
   }

UserInfo.php

  public function user()
 {
    return $this->belongsTo('User');
 }

So the question is how would I reference the fullname field in the input::old??

Help greatly appreciated.

Felix's avatar

I merged several tables/models in an array (in the Repo, Controller ...) and used that for the binding.

{{Form::model($entity_values,['route'=>['admin.entities.update',$entity->entity_id], ...

But maybe there is a more elgant way ...

tjames's avatar

To access related models using form model binding you use the dot notation, so:

 Form::text('userinfo.fullname, Input::old('userinfo.fullname'), ...);
theUnforgiven's avatar

Tried that @tjames and nothing I pass a $user variable through from my controller like so:

 $user = Auth::user();
theUnforgiven's avatar

@tjames I tried what you said userinfo.fullname and I'm not getting anything in the text input it's empty.

For the model binding the $user var here:

{{ Form::model($user, ['method' => 'PUT', 'route' => ['profile.update', $user->id], 'class' => 'form-horizontal']) }}

Is passed from my controller like so:

 $user = Auth::user();
tjames's avatar

Make sure that the field you're trying to fetch matches your model's relationship method name and the field itself. Based on your model code above it should be:

Form::text('userInfo.fullname', ... );
tjames's avatar

Then something isn't hooked up right, does the $user model that's passed to your view have access to the userInfo method, i.e. does $user->userInfo() return the correct data?

theUnforgiven's avatar

After doing {{ dd($user->userinfo) }} in my view everything is present and correct, so not sure, any other idea's?

tjames's avatar

Try using:

Form::model($user->toArray(), ...);

or

Form::text('userInfo[fullname]', ...);
theUnforgiven's avatar

Tried every which way now and still nothing, but dd($user->userinfo) returns everything associated.

tjames's avatar

Without actually seeing all of the code or being able test it in some way, there really isn't much we can do. The methods I described in my earlier posts work on my end without any issues. Is there a git repo you can link to? Or at very least, paste your model(s), controller and view here: http://laravel.io/bin and link that.

theUnforgiven's avatar

Ok in that case here's the code:

Controller: http://laravel.io/bin/NK705

User Model: http://laravel.io/bin/4Qejw

UserInfo Model: http://laravel.io/bin/n26Jl

View: http://laravel.io/bin/BkJ8d

theUnforgiven's avatar

Guys really need a hand on this please. I've posted all relevant code HELP! :(

tjames's avatar

There's multiple problems with the way you have things wired up. First, your UserInfo model needs to be named something different, the camel case model names for the most part, do not work. Change it to Profile or something since that's effectively what it's being used as.

Second, and the primary reason you're not getting any output is because you're trying to access info on the model that you're not sending to the view. All you're sending is the $user model/variable, you're not sending the userinfo along with it and the Form class only looks for data that's currently in the model, it won't run any queries AFAIK. So you need to eager load your userinfo model along with the user model. Something like this, assuming you change UserInfo to Profile:

$id = Auth::user()->id;
$user = User::with('profile')->where('id', '=', $id)->get();

Then in your view just use the array syntax:

Form::text('profile[fullname'], ...);
theUnforgiven's avatar

Tried what you said by renaming the UserInfo model to Profile and changing all the relevant code and now I get

 Undefined property: Illuminate\Database\Eloquent\Collection::$id (View: /home/vagrant/code/engine/app/views/account/profile.blade.php) 

Which is related to:

  {{ Form::model($user, ['method' => 'PUT', 'route' => ['profile.update', $user->id], 'class' => 'form-horizontal']) }}

Any idea's?

tjames's avatar
tjames
Best Answer
Level 3

From the error it looks like you're not passing the proper model/data to the view. Have you gone through and double checked the code to make sure it's correct?

  1. Your UserInfo.php model should now be Profile.php with the class name Profile.
  2. Your User model's userInfo() method should now look like this:

    public function profile() {
       return $this->hasOne('Profile', 'user_id'); 
    }
    
  3. Your controller's index method should look like this:

    public function index() { $id = Auth::user()->id; $user = User::with('profile')->find($id); // or using the where() method I described above return View::make('account.profile', compact('user')); }

  4. And the call in your view:

    {{ Form::text('profile[fullname]', Input::old('profile[fullname]'), array('class'=>'form-control')) }}
    

Now unless you've changed something else or your tables are structured differently than you originally mentioned, I can guarantee you this works as I set up a test project last night just to try it out.

theUnforgiven's avatar

Looks like it was the "find($id)" part that was missing, all working now, thanks for the extra pair of eyes and brains, much appreciated.

theUnforgiven's avatar

So now I have to update the field in the DB, I have html

 <input class="form-control" name="profile[fullname]" type="text" value="john doe">

So how do i grab the name="" field now as Input::get('fullname'); doesn't work.

Next

Please or to participate in this conversation.