代码之家  ›  专栏  ›  技术社区  ›  rpm192

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

  •  1
  • rpm192  · 技术社区  · 6 年前

    我将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');
        }
    }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   rpm192    6 年前

    好吧,在尝试了更多的东西之后,我发现我只能输入身份证,剩下的由拉拉维尔负责。

    不知道为什么我以前没试过,但问题已经解决了。

    对于具有类似/相同问题的任何人,输入现在如下所示:

        <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>
    
        2
  •  1
  •   Manuel Eduardo Romero    6 年前

    请试着打电话 $product->category (没有())代替 $product->category() . 如果函数返回一个关系,它被视为一个属性。

    所以你可以使用 $product->category->name 请试试这个,让我知道它是如何工作的。