我将Laravel5.7与LaravelCollective/HTML一起用于表单。
我有这3个表(不包括时间戳):
供应商:
|id|name|website|
产品:
|id|name|description|product_category_id|supplier_id|sales_price|buy_price|instock|discontinued|
产品类别:
|id|name|
我试图在“编辑”视图中显示产品类别和供应商的名称,但无法这样做。
我知道我可以用
$product->product_category_id
但是我也需要这个名字。ID应该存储在数据库中,名称应该显示在编辑视图中。
我试过了:
像这样在产品模型上建立一种雄辩的关系:
public function category()
{
return $this->hasOne('App\ProductCategory');
}
然后我用
$product->category()
,它返回此错误:
类的对象照明\数据库\后续\关系\ hasone无法转换为字符串
我试过
$product->category()->name
此外,它还返回此错误:
未定义的属性:照明\数据库\后续\关系\有一个::$name
我这里缺什么?
相关代码:
我的编辑视图:
<div class="row">
<div class="col-sm-12">
<div class="box box-danger">
<div class="box-header with-border">
<h3 class="box-title">Edit product {{$product->name}}</h3>
</div>
<div class="box-body">
{!! Form::open(['action' => ['ProductsController@update', $product->id], 'method' => 'post']) !!}
<div class="row">
<div class="col-sm-12">
<div class="form-group">
{{ Form::label('name', 'Product name') }}
{{ Form::text('name', $product->name, ['class' => 'form-control', 'placeholder' => 'Product name']) }}
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
{{Form::label('description', 'Product description')}}
{{Form::textarea('description', $product->description, ['id' => 'ckeditor', 'class' => 'form-control', 'style' => 'resize: vertical', 'placeholder' => 'Product description'])}}
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
{{Form::label('product_category_id', 'Product category')}}
{{Form::select('product_category_id', $categories->pluck('name', 'id'), /* here is where it should be */, ['id' => 'select2', 'class' => 'form-control select2', 'placeholder' => 'Product category'])}}
<p>Is the category you're looking for not in this list? Create it <a target="_blank" href="/product-categories/create">here</a>.</p>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
{{Form::label('supplier_id', 'Supplier')}}
{{Form::select('supplier_id', $suppliers->pluck('name', 'id'), null, ['id' => 'select2', 'class' => 'form-control select2', 'placeholder' => 'Supplier'])}}
<p>Is the supplier you're looking for not in this list? Add them <a target="_blank" href="/suppliers/create">here</a>.</p>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
{{Form::label('sales_price', 'Sales price')}}
{{Form::number('sales_price', $product->sales_price, ['class' => 'form-control', 'placeholder' => 'Sales price'])}}
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
{{Form::label('buy_price', 'Buy-in price')}}
{{Form::number('buy_price', $product->buy_price, ['class' => 'form-control', 'placeholder' => 'Buy-in price'])}}
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
{{Form::label('instock', 'In stock')}}
{{Form::select('instock', [0 => 'No', 1 => 'Yes'], null, ['id' => 'select2', 'class' => 'form-control select2', 'placeholder' => 'In stock'])}}
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
{{Form::label('discontinued', 'Discontinued')}}
{{Form::select('discontinued', [0 => 'No', 1 => 'Yes'], null, ['id' => 'select2', 'class' => 'form-control select2', 'placeholder' => 'Discontinued'])}}
</div>
</div>
</div>
{{ Form::hidden('_method', 'PUT') }}
{{ Form::submit('Save changes', ['class' => 'pull-right btn btn-default']) }}
{!! Form::close() !!}
</div>
</div>
</div>
</div>
产品表迁移
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->mediumText('description');
$table->integer('product_category_id')->unsigned();
$table->integer('supplier_id')->unsigned();
$table->decimal('sales_price', 8, 2);
$table->decimal('buy_price', 8, 2);
$table->boolean('instock');
$table->boolean('discontinued');
$table->foreign('product_category_id')->references('id')->on('product_categories')->onDelete('cascade');
$table->foreign('supplier_id')->references('id')->on('suppliers')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
供应商表
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSuppliersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('suppliers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('website');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('suppliers');
}
}
产品类别表
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('product_categories');
}
}