【问题标题】:Form::Model eloquent bindingForm::Model eloquent 绑定
【发布时间】:2015-06-06 12:35:11
【问题描述】:

当我使用 with( 进行有说服力的查询时,我在填充输入值时遇到问题

控制器:

private $viewPath = "pages.person.";

public function edit($id)
{
    $person = Person::where('id', '=', $id)->with('Email', 'Phone', 'User', 'Client', 'JuridicPerson.TypeActivities')->firstOrFail();

    $this->setData('person', $person); // Set information to be used in the creation of `views` and `nest.views`.
    $this->setData('users', User::lists('name', 'id'), ['0' => 'Select…']); // Set information to be used in the creation of `views` and `nest.views`.

    return $this->view($this->viewPath.'edit'); // Register a new view.
}

我的观点是在我的editcreate 之间共享表格的。所以我有create.blade.phpedit.blade.php 并且都拨打了form.blade.php 这我无法更改。

我的edit.blade

{{ Form::model($person, array('method' => 'PATCH', 'class' => 'defaultForm', 'route' => array('person.update', $person->id))) }}
    @include('pages.person.form')
{{ Form::close() }}

我的form.blade

<!-- This input brings the correct value -->
<div class="row">
    <div class="col-md-12">
        {{ Form::input('name', 'name', null, ['class' => 'form-control', 'placeholder' => 'Nome', 'maxlength' => 35, 'required', 'autofocus']) }}
    </div>
</div>
<!-- This input value is empty -->
<div class="row">
    <div class="col-md-12">
        {{ Form::textarea('Client[observation]', null, ['class' => 'form-control', 'placeholder' => 'Observations']) }}
    </div>
</div>

但是如果我在我的 html 中的任何位置添加这段代码,我会在 client...中得到正确的值...

{{ $person->client }}

不确定我应该如何修复我的代码,数据是正确的,当我打印数据(上面的代码)时,输入输出正确的值(但我不能在屏幕上打印返回用户)。


我需要将正确的值打印到正确的输入中。

【问题讨论】:

  • 在您的 with() 函数中,尝试使用 client 而不是 Client 看看它是否会有所不同。
  • 就是这样!谢谢,我不知道为什么代码没有显示错误,但现在它正在工作 =) 谢谢。发布为答案,以便我接受
  • 没有错误。您的关系存储在$relations 数组中,因此$relations[Client] != $relations[client] 和表单模型绑定不会加载关系。显然您加载了Client,但尝试调用client(或相反)。
  • @JarekTkaczyk 是正确的,这正是我想要做的。是我的错误。

标签: php forms laravel eloquent blade


【解决方案1】:

在加载该关系时使用client 而不是Client

当您执行{{ $person-&gt;client }} 时,它具有加载该客户端关系以便可以使用它的副作用。

在加载关系时,您应该使用函数的名称,确切地说,它负责建立这些关系。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 2015-02-02
    • 2021-04-20
    • 2013-08-04
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    相关资源
    最近更新 更多