Question

J'ai deux produits et stocks de table.

Products
--------
id
name

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

Mon code:

class ProductsModel extends Eloquent {


protected $table = 'products';

public function stock(){

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

}

class StockModel extends Eloquent {


protected $table = 'stock';

}   

Si j'utilise:

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

ou

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

Obtenir une erreur:

Laravel throw error: Undefined property: Illuminate\Database\Eloquent\Collection::$stock

Était-ce utile?

La solution

Comme vous passez un identifiant de produit, je suppose que vous voulez une seule instance de produit.Vous pouvez essayer ceci

 $product = ProductsModel::find($id);
 $stocks = $product->stock;

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top