简体   繁体   English

Laravel模型关系问题

[英]Issue with Laravel model relationships

I'm trying to retrieve all product categories with all their respective products, one product belongs to one product category and one product category can have many products. 我正在尝试检索所有具有其各自产品的产品类别,一个产品属于一个产品类别,而一个产品类别可以具有多个产品。

When I retrieve productCategories I get the following error: 检索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))

This is my migrations file for product and categories: 这是我针对产品和类别的迁移文件:

<?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');
    }
}

And my two related models: 和我的两个相关模型:

Product: 产品:

<?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');
    }
}

ProductCategory: 产品分类:

<?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');
    }
}

First of all you need to define right keyword for hasMany relation. 首先,您需要为hasMany关系定义正确的关键字。 Change HasMany to hasMany() ; HasMany更改为hasMany() ; and Model look like this:- 和模型看起来像这样:

<?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');
          } 
    }

and second model look like this: - 第二个模型如下所示:-

<?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');
         }
    }

and Query will be look like this: - 和查询将如下所示:-

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

this query will give you all records and category of particular record. 该查询将为您提供所有记录和特定记录的类别。

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

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