Eloquent Relationship not working

Posted 9 months ago by Kaustubh

Hi There I just stucked on Eloquent Relationship. The Two table are not joining. I am getting this error SQLSTATE[42703]: Undefined column: 7 ERROR: column "quantity" does not exist LINE 1: select "quantity" from "mi_cmp_product" where "mi_cmp_produc... ^ (SQL: select "quantity" from "mi_cmp_product" where "mi_cmp_product"."id" is null and "product_id" = ABC3408804)

There are Two Table 1) Company Product & 2) Shop Inventory

  1. Company Product has product_id(PK)
  2. Shop Inventory has product_id(FK)

Company Product Model

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class CmpProduct extends Model
{
    protected $table='mi_cmp_product';
    protected $fillable=['product_id','regcity_code','company_id','category_id','sub_category_id','brand_id','hsn_id','tax_gst_id','variant_id','unit_id','alt_product_id','product_name','product_desc','product_barcode_symbol','product_alert_qty','product_track_req','product_tax_method','product_cf1','product_cf2','product_cf3','specification','lastupdated_by','lastupdated_datetime'];

    public function ShopInventory(){
        return $this->hasMany('App\ShopInventory', 'product_id', 'product_id');
    }
}

Shop Inventory Model

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class ShopInventory extends Model
{
    protected $table='mi_estshop_inventory';
    protected $fillable=['regcity_code','company_id','product_id','est_shop_id','unit_id','quantity','last_pur_price'];

    public function CmpProduct(){
        return $this->belongsTo('App\CmpProduct', 'product_id');
    }

    public function get_quantity($id)
    {
        $quantity = $this->CmpProduct()->where('product_id',$id)->pluck('quantity')->first();
        return $quantity;
    }
}

Controller

 $shop = new ShopInventory;
 return view('return_purchase',compact('shop');

View

<input type="text" name="available_qty" id="available_qty" value="{{ $shop->get_quantity($pur_items->product_id) }}">

Am i missing something??

Thanks & Regards

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.