繁体   English   中英

Laravel模型关系问题

[英]Issue with Laravel model relationships

我正在尝试检索所有具有其各自产品的产品类别,一个产品属于一个产品类别,而一个产品类别可以具有多个产品。

检索productCategories时,出现以下错误:

 Illuminate \ Database \ QueryException (42S22)
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products.product_category_id' in 'where clause' (SQL: select * from `products` where `products`.`product_category_id` in (1, 2, 3))

这是我针对产品和类别的迁移文件:

<?php


use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;


class ProductsAndCategories extends Migration
{
    public function up()
    {
        //CREATE PRODUCT CATEGORIES TABLE
        Schema::create('productcategories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('description')->nullable();
            $table->string('image')->nullable();
            $table->timestamps();
        });

        //  CREATE PRODUCTS TABLE
        Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('productcategory_id')->index();
            $table->foreign('productcategory_id')->references('id')->on('productcategories');
            $table->string('title');
            $table->string('description')->nullable();
            $table->string('body')->default('');
            $table->string('image')->nullable()->default(config('globals.dummy_image'));
            $table->boolean('isVisible')->default(true);
            $table->integer('stockLeft')->default(0);
            $table->decimal('halfPrice', 5,2)->default(0.00);
            $table->decimal('fullPrice', 5,2)->default(0.00);
            $table->decimal('finalPrice', 5,2)->default(0.00);
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('products');
        Schema::dropIfExists('productcategories');
    }
}

和我的两个相关模型:

产品:

<?php
namespace App\Models;


use Illuminate\Database\Eloquent\Model;


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

    public function productcategory()
    {
        return $this->belongsTo('App\Models\ProductCategory', 'productcategory_id');
    }
}

产品分类:

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ProductCategory extends Model
{
    protected $table = 'productcategories';

    public function products()
    {
        return $this->HasMany('App\Models\Product');
    }
}

首先,您需要为hasMany关系定义正确的关键字。 HasMany更改为hasMany() ; 和模型看起来像这样:

<?php
    namespace App\Models;
    use Illuminate\Database\Eloquent\Model;
    class Product extends Model
    {
          protected $table = 'products';
          protected $primary_key = 'product_id';
          public function productcategory()
          {
               return $this->belongsTo('App\Models\ProductCategory', 'productcategory_id');
          } 
    }

第二个模型如下所示:-

<?php
    namespace App\Models;
    use Illuminate\Database\Eloquent\Model;
    class ProductCategory extends Model
    {
         protected $table = 'productcategories';
         protected $primary_key = 'id';
         public function products()
         {
              return $this->HasMany('App\Models\Product', 'id');
         }
    }

和查询将如下所示:-

$product_list = Product::with('productcategory')->get();

该查询将为您提供所有记录和特定记录的类别。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM