Laravel Relationship problems: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation

StackOverflow https://stackoverflow.com/questions/22160580

  •  19-10-2022
  •  | 
  •  

Вопрос

I must be missing something obvious. I've got a Invoice_detail Model:

class Invoice_detail extends Eloquent {

public function products()
{
    $this->belongsTo('Product');

}
}

A Product Model:

class Product extends Eloquent {

public function invoiceDetails()
{
    $this->hasMany('Invoice_detail');
}
}

but when I try to use this:

Route::get('/', function(){

  $detail =  Invoice_detail::whereId(27)->first();

  return $detail->products;
});

I get: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation

What am I missing here?

Это было полезно?

Решение

Yep - your relationship methods should have returns on them:

public function invoiceDetails()
{
    return $this->hasMany('Invoice_detail');
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top