无法在编辑页面laravel上显示数据库值

时间:2018-12-25 20:46:32

标签: php laravel laravel-blade

我正在将Laravel 5.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,但是我也需要名称。 ID应该存储在数据库中,名称应该显示在编辑视图中。

我已经尝试过:

在产品模型上建立雄辩的关系,如下所示:

public function category()
{
    return $this->hasOne('App\ProductCategory');
}

然后我使用了$product->category(),它返回了此错误:

  

Illuminate \ Database \ Eloquent \ Relations \ HasOne类的对象无法转换为字符串

我也尝试了$product->category()->name,它返回了此错误:

  

未定义的属性:Illuminate \ Database \ Eloquent \ Relations \ HasOne :: $ 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');
    }
}

2 个答案:

答案 0 :(得分:1)

好的,因此,在尝试了一些更多的事情之后,事实证明我只可以输入ID,Laravel将负责其余的工作。

不确定为什么我以前没有尝试过,但是问题已经解决。

对于具有相似/相同问题的任何人,输入现在看起来像这样:

    <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'), $product->product_category_id, ['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'), $product->supplier_id, ['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>

答案 1 :(得分:1)

请尝试致电$product->category(不带())代替$product->category()。 如果函数返回一个关系,则该关系被视为属性。

因此您可以使用$product->category->name 请尝试一下,让我知道它是如何工作的。