I am using laravel 5.1 with transformer and json api. I had combined relationship with product model in category model. one product inserted as in many categories, so that category_id to array format. now how to relate product model's category_id using transformer with $this operater.
my category model:
`
public function products()
{
return $this->hasMany('App\Product','category_id');
}
public function product()
{
$category_id=json_decode($this->category_id);
return Product::where('category_id',$this->category_id)->select('name','description','image','price','mrp','wsprice','quantity','attributes')->get();
}`
my transformer:
public function transform(Category $category){
return [
'id' => $category->category_id,
'name' => $category->name,
'slug'=>$category->slug,
'image'=>$category->image,
'product'=>$category->product(),
'subcategory'=>$category->subcategery()
];
}`
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire