Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda