【问题标题】:Laravel: editing two tablesLaravel:编辑两个表
【发布时间】:2017-11-27 21:39:10
【问题描述】:

我想知道如何在同一个视图中编辑两个表。 我有 2 个相互关联的模型。

在我看来,我正在尝试使用已插入的值重新填充我的表单选择以更改它。

模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Products extends Model
{
protected $table = 'products';

public $primaryKey = 'id';

public $timestamps = true; 

public function user()
{
    return $this->belongsTo('App\User');
}

public function material()
{
    return $this->belongsTo('App\Material');
}

}

<?php


namespace App;

use Illuminate\Database\Eloquent\Model;

class Material extends Model
{
protected $table = 'material';

public $primaryKey = 'id';

public $timestamps = true; 

function products(){
    return $this->hasMany('App\Products');
}

}

控制器

public function edit($id)
{
    $product = Products::find($id);

    return view('products.edit')->with('product', $product);
}

查看(products.edit)

@extends('layouts.app')

@include('inc.messages')
@section('content')
<div class="container">
    <h1>Edit</h1>

    {!! Form::open(['action' => ['ProductsController@update', $product->id], 'method' => 'POST']) !!}
        <div class="form-group">
            {{Form::label('title', 'Title')}}
            {{Form::text('title', $product->title, ['class' => 'form-control', 'placeholder' => 'Title'])}}
        </div>
        <div class="form-group">
            {{Form::label('body', 'Body')}}
            {{Form::textarea('body', $product->body, ['class' => 'form-control', 'placeholder' => 'Body text'])}}
        </div>
        <div class="form-group">
            {!! Form::label('material', 'Material') !!}
            {!! Form::select('material',dd($Materials), ['class' => 'form-control']) !!}
        </div>



        {{Form::hidden('_method','PUT')}}
        {{Form::submit('Submit',['class' => 'btn btn-primary'] )}}
    {!! Form::close() !!}
</div>

@endsection

错误输出 为 foreach() 提供的参数无效(查看:C:\xampp\htdocs\material\resources\views\products\edit.blade.php)

【问题讨论】:

  • 你使用哪个 Laravel 版本?
  • 嗨,lewis4u。我正在使用 Laravel 5.4 版。

标签: laravel


【解决方案1】:

为了在 Laravel 中重新填充选择框,您需要有用户可以选择的可能选项,然后制作这样的选择框:

<div class="form-group">
    {!! Form::label('material', 'Material') !!}
    {!! Form::select('material', ['option1', 'option2', 'option3'], null, ['class' => 'form-control']) !!}
</div>

而且选项 1,2,3 数组也可以从数据库中调用。

附:你应该使用 {!! !!} 结尾而不是 {{ }} 用于 {!! Form !!} 外观。

更新


为了从数据库中获得选择框的选项,您需要使用 pluck() 来获取它们。

在你的控制器中创建一个新函数:

public function materials()
{
    return DB::table('your_table')->pluck('name', 'id');
    // or this ↓
    return ModelName::pluck('name', 'id');
}

然后在您的 createedit 函数中像这样传递它:

public function create($id)
{
    $product = Products::find($id);
    $theMaterials = $this->materials();

    return view('products.create')->with('product', $product, 'materials', $materials);
}

public function edit($id)
{
    $product = Products::find($id);
    $theMaterials = $this->materials();

    return view('products.edit')->with('product', $product, 'materials', $materials);
}

更新


{!! Form::model($product, ['method' => 'PATCH', 'action' => ['ProductController@update', $product->id]]) !!}

【讨论】:

  • 嗨,lewis4u。这些值已经存储在我的数据库中,它应该出现在表单选择中。当我在编辑视图中时,它应该使用在创建行时选择的存储值填充选择。
  • 我创建了另一个函数。将其添加到编辑功能,但仍然不知何故我收到错误:未定义变量:材料(视图:C:\xampp\htdocs\material\resources\views\products\edit.blade.php)目前我的选择形式:{! ! Form::select('material',$Materials, ['class' => 'form-control']) !!}
  • 尝试 dd($Materials) 并确保您从控制器传递它
  • 哦,请向您的表单展示它的外观。只是编辑和创建的第一行
  • 是的,你需要使用 Form::model 这样的东西来编辑 {!! Form::model($article, ['method' =&gt; 'PATCH', 'action' =&gt; ['ArticlesController@update', $article-&gt;id]]) !!}
猜你喜欢
  • 2013-11-22
  • 2019-02-02
  • 1970-01-01
  • 1970-01-01
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多