Laravel 4 热切加载“未定义的属性”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17748655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:09:46  来源:igfitidea点击:

Laravel 4 Eager Loading "Undefined Property"

phplaravellaravel-4

提问by Jared Meyering

Why can't I access the property in an eager loaded senario?

为什么我不能在急切加载的场景中访问该属性?

I am trying to access the photo location for my facilities model. They have a Many:Many relationship. When I load the facility info using

我正在尝试访问我的设施模型的照片位置。他们有一个多:多的关系。当我使用加载设施信息时

$facilities = Facility::with('photos')->get();

When I try to access

当我尝试访问时

foreach($facilities as $facility)
{
    echo $facility->photos->id;
}

I get the Undefined property: Illuminate\Database\Eloquent\Collection::$id

我得到 Undefined property: Illuminate\Database\Eloquent\Collection::$id

If I echo $facilities->photosI end up with.

如果我回应$facilities->photos我最终。

[{"id":"3",
"name":null,
"location":"facilities\/facility1.jpg\r",
"created_at":"2013-07-18 14:15:19",
"deleted_at":null,
"updated_at":null,
"pivot":{"facility_id":"5","photo_id":"3"}}]

Whats the best way to access any property in this array?

访问此数组中任何属性的最佳方法是什么?

回答by Altrim

With eager loading the photosyou get a photoscollection and in order to get the photo you need to iterate through the photoscollection e.g.

随着急切加载,photos你会得到一个photos集合,为了得到照片,你需要遍历photos集合,例如

foreach($facilities as $facility)
{

 foreach($facility->photos as $photo)
 {
    echo $photo->id;
 }

}

If you only want just the first photo you can get it by calling the first()method on the collection

如果你只想要第一张照片,你可以通过调用first()集合上的方法来获取它

foreach($facilities as $facility) 
{
  $facility->photos->first()->id;
}

回答by sidneydobber

What is received from the database is an array with one object. This is why a foreach works. To access it the way you want you should add the first() method just as Atrim says, you can even drop the get() method. Only I would suggest doing it in the controller.

从数据库接收到的是一个包含一个对象的数组。这就是 foreach 起作用的原因。要以您想要的方式访问它,您应该像 Atrim 所说的那样添加 first() 方法,您甚至可以删除 get() 方法。只有我会建议在控制器中进行。

$facilities = Facility::with('photos')->first();

{{ $facilities->location }}

回答by AZReed

Thanks Altim, I just hit this problem too and the nested foreach is one of the solutions. Nevertheless, I'll share with you guys the solution that I used at the end.

谢谢 Altim,我也遇到了这个问题,嵌套的 foreach 是解决方案之一。尽管如此,我还是会与大家分享我最后使用的解决方案。

Instead of the nested foreach or even the eager loader, I used a Join Query Builder. In my case:

我使用了 Join Query Builder,而不是嵌套的 foreach 甚至是eager loader。就我而言:

$posts = DB::table('posts')
                ->leftJoin('users', 'posts.user_id', '=', 'users.id')
                ->get();

This allowed me to use one foreach, and all the data is at the same level, in my case for instance would be something like:

这使我可以使用一个 foreach,并且所有数据都处于同一级别,例如在我的情况下将是这样的:

@foreach ($posts as $post)
  <p>{{ $post->first_name }}</p> # From users table
  <p>{{ $post->title }}</p> # From posts table
  <p>{{ $post->body }}</p>
@endforeach

Maybe it is not answering this question exactly , but hope it helps to others like me that ended up in this particular question.

也许它并没有完全回答这个问题,但希望它对像我这样最终解决这个特定问题的其他人有所帮助。