我想知道如何在同一视图中编辑两个表。 我有两个与彼此相关的模型。
在我看来,我正在尝试使用已插入的值重新填充表单选择以便更改它。
模型
<?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)