php 此集合实例上不存在属性 [title]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41366092/
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-08-26 02:23:29  来源:igfitidea点击:

Property [title] does not exist on this collection instance

phplaravellaravel-eloquent

提问by zkanoca

I am following Laracasts' videos: Basic Model/Controller/View Workflow.

我正在关注 Laracasts 的视频:Basic Model/Controller/View Workflow

I have a table holds contact information.

我有一张桌子保存联系信息。

CREATE TABLE `about` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`content` text COLLATE utf8_unicode_ci,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

I am trying to pass data to view using the following code in the controller file:

我正在尝试使用控制器文件中的以下代码将数据传递给视图:

public function index()
{
    $about = Page::where('page', 'about-me')->get(); //id = 3

    return view('about', compact('about'));
}

When I try to show the code as shown below,

当我尝试显示如下所示的代码时,

@section('title')
    {{$about->title}}
@stop

@section('content')
    {!! $about->content !!}
@stop

I get error that says:

我收到错误消息:

Property [title] does not exist on this collection instance. (View: E:\laragon\www\newsite\resources\views\about.blade.php)

此集合实例上不存在属性 [title]。(查看:E:\laragon\www\newsite\resources\views\about.blade.php)

But if I change the retrieving method in the controller file, it works.

但是如果我更改控制器文件中的检索方法,它就可以工作。

public function index()
{
    $about = Page::find(3);

    return view('about', compact('about'));
}

When I use dd($about)in the first case (where()->get()) the data is encapsulated by an array. In the second case (find(3)) it displays data as expected.

当我dd($about)在第一种情况 ( where()->get()) 中使用时,数据被一个数组封装。在第二种情况 ( find(3)) 中,它按预期显示数据。

What am i doing wrong?

我究竟做错了什么?

回答by Alexey Mezenin

When you're using get()you get a collection. In this case you need to iterate over it to get properties:

当您使用时,get()您会得到一个集合。在这种情况下,您需要对其进行迭代以获取属性:

@foreach ($collection as $object)
    {{ $object->title }}
@endforeach

Or you could just get one of objects by it's index:

或者你可以通过它的索引获取一个对象:

{{ $collection[0]->title }}

Or get first object from collection:

或者从集合中获取第一个对象:

{{ $collection->first() }}

When you're using find()or first()you get an object, so you can get properties with simple:

当您使用find()first()获得object 时,您可以通过简单的方式获得属性:

{{ $object->title }}

回答by Alex

With get()method you get a collection (all data that match the query), try to use first()instead, it return only one element, like this:

使用get()方法你得到一个集合(与查询匹配的所有数据),尝试使用first()它,它只返回一个元素,如下所示:

 $about = Page::where('page', 'about-me')->first();

回答by Pnj Patel

You Should Used Collection keyword in Controller. Like Here..

您应该在 Controller 中使用 Collection 关键字。像这儿..

public function ApiView(){
    return User::collection(Profile::all());
}

Here, User is Resource Name and Profile is Model Name. Thank You.

这里,用户是资源名称,配置文件是模型名称。谢谢你。

回答by Phantih

$about = DB::where('page', 'about-me')->first(); 

in stead ofget().

代替get().

It works on my project. Thanks.

它适用于我的项目。谢谢。

回答by Onyash Ed

$about->first()->idor $stm->first()->titleand your problem is sorted out.

$about->first()->id或者 $stm->first()->title,您的问题已解决。

回答by agm1984

A person might get this while working with factory functions, so I can confirm this is valid syntax:

一个人在使用工厂函数时可能会得到这个,所以我可以确认这是有效的语法:

$user = factory(User::class, 1)->create()->first();

You might see the collection instance error if you do something like:

如果您执行以下操作,您可能会看到集合实例错误:

$user = factory(User::class, 1)->create()->id;

so change it to:

所以将其更改为:

$user = factory(User::class, 1)->create()->first()->id;