拉拉维尔表单模型绑定一对一关系未填充


Laravel Form Model Binding One to One Relationships Not Populated

术语表:

  • 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() }}
并且所有输入文本都没有填充,

已经尝试了很多谷歌搜索的方法,但没有任何效果,那么如何在我的输入文本和文本区域中填充数据? 我将其用于编辑功能。

谢谢,我是拉拉维尔的新手。 任何帮助都将得到赞赏。

首先,必须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']) }}

在我的情况下,没有在文本区域上呈现模型值。

因此,我给出的值不是 null,如下所示:

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

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

我也有同样的问题,但我这样做了:

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

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

只要得到一个想法。希望对您有所帮助!