Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
Accessing One to many relation giving non static methode should not be accessed stitically error Laravel5.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

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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');
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!