![Eigentum Property Property [title] does not exist on this collection instance does not exist on this collection instance ist in dieser Sammlungsinstanz nicht vorhanden 1 Eigentum Property Property title does not exist on this collection](https://www.viresist.org/wp-content/uploads/Eigentum-Property-Property-title-does-not-exist-on-this-collection.jpg)
zkanoca
Ich verfolge die Videos von Laracasts: Grundlegender Modell-/Controller-/View-Workflow.
Ich habe eine Tabelle mit Kontaktinformationen.
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
Ich versuche, Daten zur Ansicht mit dem folgenden Code in der Controller-Datei zu übergeben:
public function index()
{
$about = Page::where('page', 'about-me')->get(); //id = 3
return view('about', compact('about'));
}
Wenn ich versuche, den Code wie unten gezeigt anzuzeigen,
@section('title')
{{$about->title}}
@stop
@section('content')
{!! $about->content !!}
@stop
Ich bekomme eine Fehlermeldung, die besagt:
Eigentum Property Property [title] does not exist on this collection instance does not exist on this collection instance ist in dieser Sammlungsinstanz nicht vorhanden. (Ansicht: E:\laragon\www\newsite\resources\views\about.blade.php)
Aber wenn ich die Abrufmethode in der Controller-Datei ändere, funktioniert es.
public function index()
{
$about = Page::find(3);
return view('about', compact('about'));
}
Wenn ich benutze dd($about)
im ersten Fall (where()->get()
) werden die Daten von einem Array gekapselt. Im zweiten Fall (find(3)
) zeigt es Daten wie erwartet an.
Was mache ich falsch?
![Eigentum Property Property [title] does not exist on this collection instance does not exist on this collection instance ist in dieser Sammlungsinstanz nicht vorhanden 2 Eigentum Property Property title does not exist on this collection](https://www.viresist.org/wp-content/uploads/Eigentum-Property-Property-title-does-not-exist-on-this-collection.png)
Alexey Mezenin
Wenn Sie verwenden get()
du bekommst ein Sammlung. In diesem Fall müssen Sie darüber iterieren, um Eigenschaften zu erhalten:
@foreach ($collection as $object)
{{ $object->title }}
@endforeach
Oder Sie könnten einfach eines der Objekte anhand seines Index abrufen:
{{ $collection[0]->title }}
Oder holen Sie sich das erste Objekt aus der Sammlung:
{{ $collection->first() }}
Wenn Sie verwenden find()
oder first()
du bekommst ein Objektsodass Sie Eigenschaften mit einfachem abrufen können:
{{ $object->title }}
![Eigentum Property Property [title] does not exist on this collection instance does not exist on this collection instance ist in dieser Sammlungsinstanz nicht vorhanden 3 Eigentum Property Property title does not exist on this collection](https://www.viresist.org/wp-content/uploads/Eigentum-Property-Property-title-does-not-exist-on-this-collection.gif)
Alex
Mit get()
Methode erhalten Sie eine Sammlung (alle Daten, die der Abfrage entsprechen), versuchen Sie zu verwenden first()
Stattdessen gibt es nur ein Element zurück, etwa so:
$about = Page::where('page', 'about-me')->first();
![Eigentum Property Property [title] does not exist on this collection instance does not exist on this collection instance ist in dieser Sammlungsinstanz nicht vorhanden 4 1646912046 110 Eigentum Property Property title does not exist on this collection](https://www.viresist.org/wp-content/uploads/1646912046_110_Eigentum-Property-Property-title-does-not-exist-on-this-collection.png)
Phantih
$about = DB::where('page', 'about-me')->first();
anstatt get()
.
Bei meinem Projekt funktioniert es. Danke.
Eine Person könnte dies beim Arbeiten mit Factory-Funktionen erhalten, daher kann ich bestätigen, dass dies eine gültige Syntax ist:
$user = factory(User::class, 1)->create()->first();
Möglicherweise wird der Sammlungsinstanzfehler angezeigt, wenn Sie Folgendes tun:
$user = factory(User::class, 1)->create()->id;
also ändere es zu:
$user = factory(User::class, 1)->create()->first()->id;
Sie sollten das Sammlungsschlüsselwort im Controller verwenden. Wie hier..
public function ApiView(){
return User::collection(Profile::all());
}
Hier ist der Benutzer der Ressourcenname und das Profil der Modellname. Danke.
![Eigentum Property Property [title] does not exist on this collection instance does not exist on this collection instance ist in dieser Sammlungsinstanz nicht vorhanden 5 1646912046 379 Eigentum Property Property title does not exist on this collection](https://www.viresist.org/wp-content/uploads/1646912046_379_Eigentum-Property-Property-title-does-not-exist-on-this-collection.jpg)
Onyash Ed
$about->first()->id
oder
$stm->first()->title
und dein Problem ist gelöst.
Als Ergebnis $about = Page::where('page', 'about-me')->get()
ist eine Laravel-Sammlung, Sie können ein bestimmtes Attribut wie erhalten Titel verwenden pluck()
Methode, die in beschrieben ist https://laravel.com/docs/master/collections#method-pluck.
$titles = $about->pluck( 'title' )
@foreach($titles as $title)
{{ $title }}
@endforeach
9877500cookie-checkEigentum Property Property [title] does not exist on this collection instance does not exist on this collection instance ist in dieser Sammlungsinstanz nicht vorhandenyes