Question

I have two table PRODUCTS and STOCK.

Products
--------
id
name

Stock
------- 
id
products_id
organization
price

my code:

class ProductsModel extends Eloquent {


protected $table = 'products';

public function stock(){

    return $this->hasMany('StockModel', 'products_id');  

}

class StockModel extends Eloquent {


protected $table = 'stock';

}   

If I use:

$data['products'] = ProductsModel::where('id', $id)->stock()->where('organization', 1)->get();

Or

$data['products'] = ProductsModel::where('id', $id)->stock->where('organization', 1)->get();

get error:

Laravel throw error: Undefined property: Illuminate\Database\Eloquent\Collection::$stock
Was it helpful?

Solution

As you are passing a product id I assume you want just one product instance. You can try this

 $product = ProductsModel::find($id);
 $stocks = $product->stock;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top