php - Laravel 表单模型绑定(bind)未填充的一对一关系

标签 php laravel laravel-4

术语表:

  • term_id
  • 姓名
  • 蛞蝓

术语分类表:

  • term_taxonomy_id
  • term_id
  • 描述

我的学期模型:

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

我的术语分类模型:

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

我的 Controller

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, ['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']) }}

关于php - Laravel 表单模型绑定(bind)未填充的一对一关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28131322/

相关文章:

validation - 在 Laravel 表单验证中为多个字段设置一条自定义消息

php - Laravel4 POST 无法解释的重定向到 GET

javascript - 使用 Laravel/Ajax 混淆 javascript 中的请求 url

java - PHP 或 Java 中的 WCMS(针对 Java 开发人员)?

javascript - Laravel-如果我将变量传递给 Controller ​​并保存在数据库中,如何在 javascript 中使用 switch case

php - DokuWiki 登录页面编辑

php - 如何在碳 Laravel 中添加另一个日期的日期?

Laravel hasMany通过多态关系

php - 加载数据本地 Infile 不将数据添加到数据库

php - Doctrine postSave、postUpdate 和国际化(检测修改)