Relationship in Laravel/Eloquent ORM

Asked

Viewed 759 times

3

I have a problem. I need to return the values of a relationship, however, presents the following error:

ErrorException
Undefined property: Illuminate\Database\Eloquent\Collection::$fileServico

My code:

Fileclass.php

public function fileServico(){
    return $this->hasMany('File');
}

Fileservico.php

public function file(){
    return $this->belongsTo('File');
}

You can have multiple Fileservico for a Fileclass. What am I calling:

Reservacontroller.php

public function getIndex(){
    return View::make('home')->with('file',FileClass::all()->fileServico);
}
  • Ewerton Melo, put in your question the two tables and the classes in full please! Your relationship is wrong, but as I don’t know the name of your class it is difficult to put the answer

1 answer

2

That

FileClass::all()->fileServico

It doesn’t work. In case to catch the fileServico of each FileClass, would have to be in an iteration:

foreach( FileClass::all() as $item ){
    var_dump( $item->fileservico );
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.