Laravel渴望加载和表单模型绑定 [英] Laravel Eager Loading and Form Model Binding

查看:95
本文介绍了Laravel渴望加载和表单模型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户模型,并通过Eager加载,我也在加载表配置文件.

I have a User model and via Eager Loading i am loading also the Table Profile.

要显示它,我只需像平常一样写$user->profile->nameOfTheProfilePropertyIWant.

To Display it i just write $user->profile->nameOfTheProfilePropertyIWant just as normal.

简单.

但是我现在到底该如何使用表单模型绑定? 正常情况下,我会将Form::text('nameOfTheUserPropertyIWant')用于诸如电子邮件之类的用户属性.但是我想在配置文件的属性上设置类似的位置. (配置文件->位置)

But how exactly do i use Form Model Binding now? Normal i would use Form::text('nameOfTheUserPropertyIWant') for a User Property like an email. But i want to set in on a Property of the Profile like the location. (profile->location)

但是如何将其设置为个人资料属性?

But how can i set it to an profile property?

推荐答案

您可以使用以下方式(以one-to-one关系)进行此操作:

You may do this (in a one-to-one relationship) using something like this:

Form::text('profile[location]')

在这种情况下,如果将User模型的实例绑定到与相关模型profile一起使用的Form::model($user),则将在给定的文本框中填充$user->profile->location.

In this case $user->profile->location will be populated in the given text box if an instance of User model is bound to the from using Form::model($user) with the related model profile.

这篇关于Laravel渴望加载和表单模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆