[英]Laravel 5.2 injecting object into Blade template
I am binding an employee model into a Blade
template, and want to place the result of an eager load relation into a field. 我将一个员工模型绑定到Blade
模板中,并希望将急切的负载关系的结果放入字段中。
In my controller I build the collection for the page as: 在我的控制器中,我将页面的集合构建为:
$employee = User::with('country', 'activeOrganisationRole')->first();
My form open statement is: 我的表单打开语句是:
{!! Form::model($employee, ['route' => ['employee.role', $employee->uuid], 'method' => 'POST']) !!}
So I want to populate $employee->country->name
into an input Laravel Collective form::text
statement, but I cannot get the country name to load. 因此,我想将$employee->country->name
Laravel Collective form::text
$employee->country->name
填充到输入Laravel Collective form::text
语句中,但是无法加载国家名称。 All other fields on the form load perfectly from the parent collection. 表单上的所有其他字段都可以完美地从父集合加载。
My Country field is: 我的国家字段为:
<div class="form-group">
<label for="phone" class="control-label">Country</label>
{!! Form::text('country', null, ['id'=>'country', 'placeholder' => 'Country', 'class' => 'form-control']) !!}
</div>
The above country field loads the entire relation result into the input. 上述国家/地区字段将整个关系结果加载到输入中。 What is the correct syntax for injecting $employee->country->name
into this input? injecting $employee->country->name
注入此输入的正确语法是什么?
By the way , this works perfectly, but I have learned nothing by doing this way! 顺便说一句,这很完美,但是通过这种方式我学不到任何东西!
<label for="title" class="control-label">Country</label>
<input id="country" class="form-control" value="{!! $employee->country->country !!}" readonly>
I believe the FormBuilder
in LaravelCollective uses data_get
(a Laravel helper function) to get attributes from objects. 我相信LaravelCollective中的FormBuilder
使用data_get
(一个Laravel帮助函数)从对象获取属性。 However, dots in names of elements is kind of strange so I delved a little into the source for you. 但是,元素名称中的点有点奇怪,因此我为您深入研究了源代码。
You have one of the following choices (ordered by my preference): 您有以下选择之一(按我的喜好排序):
You can add a method called getFormValue
in your Employee model. 您可以在Employee模型中添加一个名为getFormValue
的方法。 This takes a single parameter which is the name of the form element that is requesting a value. 它采用一个参数,该参数是请求值的表单元素的名称。 Implement it something like this: 实现它是这样的:
public function getFormValue($name) { if(empty($name)) { return null; } switch ($name) { case 'country': return $this->country->country; } // May want some other logic here: return $this->getAttribute($name); }
I couldn't really find any documentation on this (such is sometimes the way with Laravel). 我真的找不到关于此的任何文档(Laravel有时就是这样)。 I only found it by trawling the source - although it really is easy with PhpStorm Shamless Plug 我只是通过搜寻源代码找到它的-尽管使用PhpStorm Shamless Plug确实很容易
The downside with this is that you lose the transformation and attempt at extracting values from your employee object with data_get
. 这样做的不利之处是您会丢失转换,并尝试使用data_get
从雇员对象中提取值。
Change the name of your text field to be country[country]
. 将您的文本字段的名称更改为country[country]
。 In the source the builder replaces '[' and ']' with '.' 在源代码中,构建器将'['和']'替换为'。 and '' respectively when looking in an object for an attribute. 在对象中查找属性时分别使用“和”。 This means data_get
will be looking for country.country
. 这意味着data_get
将寻找country.country
。
I put this here for people with the issue in the future, but it's not recommended . 我将其放在此处供将来遇到此问题的人使用,但不建议这样做 。
Give your employee model a getCountryAttribute
method. 给您的员工模型一个getCountryAttribute
方法。 As explained under the "Form Model Accessors" heading in the documentation , you can overwrite what gets returned from $employee->country. 如文档中 “表单模型访问器”标题下所述 ,您可以覆盖从$ employee-> country返回的内容。 This means you can't access the real object though. 这意味着您无法访问真实对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.