简体   繁体   中英

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:

 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. Change HasMany to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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