0
我在這裏有一些非常奇怪的問題與laravel一對多關係。我在用戶和書籍之間有一對多的關係。當試圖從視圖中顯示相關的對象時,結果是none或者相關的對象,這取決於我如何訪問它。laravel 4雄辯:一對多導致奇怪的結果
用戶模型
//User table: id, username, ...
class User extends ConfideUser implements UserInterface, RemindableInterface {
public function books(){
return $this->hasMany("Book","user");
}
}
Book模型
//Book table: id, user, title...
class Book extends Ardent{
public function myUser(){
return $this->belongsTo("User","user"); //I name user_id field as "user"
}
}
觀點:
@if(! empty($book->myUser)) //It is always empty
@else
{{$book->myUser}} //It displays the user object
@endif
{{$book->myUser->id}} //ErrorException: Trying to get property of non-object
{{$book->myUser["id"]}} //This works