Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
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

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

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');
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda