【问题标题】:Laravel Form Model Binding One to One Relationships Not PopulatedLaravel 表单模型绑定一对一关系未填充
【发布时间】:2015-01-24 23:10:26
【问题描述】:

术语表:

  • term_id
  • 姓名
  • 蛞蝓

Term_taxonomy 表:

  • term_taxonomy_id
  • term_id
  • 说明

我的术语模型:

public function TermTaxonomy(){
    return $this->hasOne('TermTaxonomy');
}

我的术语分类模型:

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

我的控制器

public function edit($id)
    {
    $categories = Term::with(['TermTaxonomy' => function($q){
        $q->select('term_id', 'description');
    }])->get(['term_id', 'name', 'slug']);

    $category = Term::with(['TermTaxonomy' => function($q){
        $q->select('term_id', 'description');
    }])->find($id, ['term_id', 'name', 'slug']);
    return View::make('qhymchildz.backend.posts.categories', compact('category', 'categories'));
}

我的观点

        @if (isset($category))
        {{ Form::model($category, ['route' => ['admin_posts_categories_update', $category->term_id], 'method' => 'PATCH']) }}
    @else
        {{ Form::open(['route' => 'admin_posts_categories_store'])}}
    @endif 

        {{ Form::label('name', 'Name') }}
        <span data-tooltip aria-haspopup="true" class="has-tip radius" title="Category name for your posts.">
        {{ Form::text('name', '', ['placeholder' => 'Category name here']) }}
        @if ($errors->has('name')) <small class="error"> {{ $errors->first('name') }} </small> @endif
        </span>

        {{ Form::label('slug', 'Slug') }}
         <span data-tooltip aria-haspopup="true" class="has-tip radius" title="Slug or URL for your category.">
        {{ Form::text('slug', '', ['placeholder' => 'Slug here']) }}
        @if ($errors->has('slug')) <small class="error"> {{ $errors->first('slug') }} </small> @endif
        </span>

        {{ Form::label('description', 'Description') }}
        {{ Form::textarea('description', '', ['placeholder' => 'Category description here', 'size' => '50x5']) }}

    @if (!isset($category))        
        {{ Form::submit('Add New Category', ['class' => 'radius button']) }}
    @else
        {{ Form::submit('Update', ['class' => 'radius button']) }}
    @endif

    {{ Form::close() }}

并且所有输入文本都没有填充,已经尝试了很多谷歌搜索但没有任何工作,那么如何在我的输入文本和文本区域中填充数据?我用它来编辑功能。

谢谢,我是 laravel 的新手。任何帮助将不胜感激。

【问题讨论】:

  • 尝试传递null而不是空字符串作为值:{{ Form::text('name', null, [...]) }}
  • 很高兴再次见到你@lukasgeiter 现在名称和 slug 输入文本已填充,但描述仍然空白,因为它在一对一的关系和不同的表中。已经尝试了很多为什么但没有工作。那么如何填充描述?非常感谢。

标签: php laravel laravel-4


【解决方案1】:

首先,第二个参数必须是null,因此它实际上会使用模型中的值:

{{ Form::text('name', null, ['placeholder' => 'Category name here']) }}

要使用相关模型中的属性,您可以使用:

{{ Form::textarea('TermTaxonomy[description]', null, ['placeholder' => 'Category description here', 'size' => '50x5']) }}

注意,开头不需要@if(isset($category))Form::model 方法将自行处理不存在的模型。这就足够了:

{{ Form::model($category, ['route' => ['admin_posts_categories_update', $category->term_id], 'method' => 'PATCH']) }}

【讨论】:

  • 是的,它可以显示描述,但是当我尝试添加新类别时,我得到 undefined index: description 并指向这一行 $TermTaxonomy-&gt;description = $data['description']; 然后我更改为 $TermTaxonomy-&gt;description = $data['TermTaxonomy']['description']; 和它的工作,这是正确的方式 ?在我删除isset() 后,它会正确显示表单并正确更新按钮和保存按钮,如何做到这一点?因为我删除了isset(),因为我知道要检测的是更新还是添加新的我们需要使用如果,如果编辑则显示编辑表单和更新按钮,否则添加新表单并添加新按钮@lukasgeiter
  • 是的,像您描述的那样访问它是正确的。此外,您仍然需要 isset() 提交按钮
  • 我没有在提交按钮中使用 isset(),但它的工作方式与预期的一样,不知道为什么。 @lukasgeiter
【解决方案2】:

在我的情况下,没有在 Textarea 上呈现模型值。

所以我给出的值不是 null,而是这样的:

{{ Form::textarea('TermTaxonomy[description]', $category->TermTaxonomy['description'], ['placeholder' => 'Category description here', 'size' => '50x5']) }}

希望这也对某人有所帮助。

【讨论】:

    【解决方案3】:

    我也遇到了同样的问题,但我是这样做的:

    {!! Form::text('firstname', $user->profile->firstname, ['class' => 'form-control']) !!}
    

    就我而言: 用户belongsTo('App\Profile'), 简介hasOne('App\profile')

    想一想。希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 2016-06-11
      • 2014-12-31
      • 2014-12-06
      • 1970-01-01
      • 2016-02-23
      • 2018-10-08
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      相关资源
      最近更新 更多