Laravel 5 has one link to form binding

I spent quite a good two days on something so simple and small. I have a model called User, which has one relationship to user notes.

On the user model side, I have a specific toTo relationship, and then the user model defines the hasOne side of the relationship.

The form I use is bound to the $user model, while the UserNote model has its own table, which maps to user_id.

I am trying to get what is shown below on the right;

 {{ Form::textarea($user->notes, null , [ 'class' => 'form-control', 'placeholder' => 'Note Content']) }} 

Is there anyone to help me figure this out? b All I need to be able to add a note, and if the user does not have a note, I should not get errors, because if I do as shown below, I get an error

 {{ Form::textarea('UserNote[content]',... }} 

Your advice will be appreciated.

 class User{ ... public function note() { return $this->belongsTo(UserNote::class); } } class UserNote{ protected $fillable = ['content', 'user_id']; ... public function user() { return $this->hasOne(User::class, 'user_id'); } } 

Of course, the user_id in $fillable should not be there in the first place, because this means that I can update this table manually, whereas I want everything to be done automatically from the controller to form the binding.

0
source share
1 answer

First of all, you are not right about relationships.

  • HasOne UserNote
  • UserNote belongs to the user.

So, you have to swap relationships on the respective models.
Secondly, the textarea form has a list of parameters, for example:

 public function textarea($name, $value = null, $options = []){} 

So, the first parameter is obviously form_name . the second parameter will be the input value . You are doing it wrong.
In your case, it should be (as I think)

 {{ Form::textarea('user_note', $user->note , [ 'id' => 'user_note', 'class' => 'form-control', 'placeholder' => 'Note Content']) }} 

Edit
The note property is the name of the method that you previously wrote as notes; the method does not exist. Edit endings
Hope this helps. Happy coding!

0
source share

All Articles