3

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

Mutasim Fuad
  • 606
  • 2
  • 12
  • 31
  • Possible duplicate of [Error message Strict standards: Non-static method should not be called statically in php](http://stackoverflow.com/questions/4684454/error-message-strict-standards-non-static-method-should-not-be-called-staticall) – miken32 Apr 21 '17 at 17:06

1 Answers1

3

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');
BetaDev
  • 4,516
  • 3
  • 21
  • 47