I have two model Products and Photo. These models are followings
Photo Model
public function Product(){
return $this->belongsTo('App\Product');
}
Product Model
public function Photo(){
return $this->hasMany('App\Photo');
}
I need to get all the photos of a product catagory. For this i have tried this
$Photo = Products::Photo()->where(['catagory' => $request->catagory])->get();
But I'm getting the following error.
ErrorException in IndexController.php line 49: Non-static method App\Product::Photo() should not be called statically
How to solve it? And what is the problem
Use the something like following to access the Photos of a category of the product.
$product = Products::where(['catagory' => $request->catagory])->with('Photo')->get();
$photo = $product->Photo;
var_dump($photo);
See Photo(); method of the Product class is not the method that you can access directly via the class name (Static way of accessing method of a class).
You are try to call the method of a class just like as we do with static method for example
ClassName::Method_name()
But your Photo method is not static neither the class.
Update: For relation, its better to put (Optional) the related foreign key which establishes the relationship
return $this->hasMany('App\Photo','your_foreign_key_for_this_relation');