Controller code: {code...} How can I write in viewshowuser. blade. php to display the value of the person object? Only {$ person [0]-& amp; gt; id }}??? The displayed value is an array. Not an object. View ()-& amp; gt; withX... Controller code:
public function show(Request $request,$id){$person = DB::table('persons')->where('id',$id)->get();return view('showuser',compact('person'));}
How can I write view/showuser. blade. php to display the value of the person object?
It must be written in this way.{{ $person[0]->id }} ???
The displayed value is an array. Not an object.
View ()-> withXXX
View ('xxx, compact ('xxx '))
How should all the data passed to the view be displayed ???
What is the form of the data?
Hope you are familiar with laravel ~
Reply content:
Controller code:
public function show(Request $request,$id){$person = DB::table('persons')->where('id',$id)->get();return view('showuser',compact('person'));}
How can I write view/showuser. blade. php to display the value of the person object?
It must be written in this way.{{ $person[0]->id }} ???
The displayed value is an array. Not an object.
View ()-> withXXX
View ('xxx, compact ('xxx '))
How should all the data passed to the view be displayed ???
What is the form of the data?
Hope you are familiar with laravel ~
$person = DB::table('persons')->where('id',$id)->get();
In fact, a Collection object is retrieved from the Builder object, and the Collection object contains the Person object. Collection implements array-related interfaces, so you can also access the array to obtain the Person object.
If you want to directly obtain the Person object by id, you can use the find method:
$person = DB::table('person')->find($id);
In Blade, you can use @ for, @ foreach, or @ forelse to traverse object sets, and then display the object property values:
@forelse($persons as $person) {{ $person->name }} @empty Nothing.@endforelse
You can directly access a single object:
Hello, {{ $person->name }} !
Since the owner has written a user who obtains an ID, it cannot be used at all.foreach
Now. Only one user can be obtained.first()
Method:
$person = DB::table('persons')->where('id',$id)->first();// Or$person = DB::table('persons')->where('id',$id)->firstOrFail();
In this way, you can directly$person->name
Instead$person[0]->name
.
You need @ foreach
Upstairs Positive Solution
@foreache($person as $temp) {$temp->foo}@endforeach
You can try to query the data format
dd();
The data transmitted in laravel is generally in the JSON array format.
Learn laravel at the same time. If you have any mistakes, please note: