Controller Code:
public function show(Request $request,$id){$person = DB::table('persons')->where('id',$id)->get();return view('showuser',compact('person'));}
How do I write in view/showuser.blade.php to show the value of this object?
It has to be written like that.{{ $person[0]->id }} ???
It feels like this is a set of numbers. Not an object.
View ()->withxxx
View (' Xxx,compact (' xxx '))
How should the data passed to the view be displayed???
What are the forms of these data?
Hope to be familiar with the laravel of the next ~
Reply content:
Controller Code:
public function show(Request $request,$id){$person = DB::table('persons')->where('id',$id)->get();return view('showuser',compact('person'));}
How do I write in view/showuser.blade.php to show the value of this object?
It has to be written like that.{{ $person[0]->id }} ???
It feels like this is a set of numbers. Not an object.
View ()->withxxx
View (' Xxx,compact (' xxx '))
How should the data passed to the view be displayed???
What are the forms of these data?
Hope to be familiar with the laravel of the next ~
$person = DB::table('persons')->where('id',$id)->get();
This is actually retrieving a Collection object from the Builder object, and the Collection object contains the person object. Collection implements an array-related interface, so you can also get the person object by accessing the array.
If you want to get the person object directly from the ID, you can use the Find method:
$person = DB::table('person')->find($id);
Blade can iterate through the collection of objects using @for, @foreach, or @forelse, and then display the object's property values:
@forelse($persons as $person) {{ $person->name }} @empty Nothing.@endforelse
For a single object, natural direct access can be:
Hello, {{ $person->name }} !
Since the landlord has written to get a user ID, it is not used at all. foreach
To get only one user can use the first()
method directly:
$person = DB::table('persons')->where('id',$id)->first();// Or$person = DB::table('persons')->where('id',$id)->firstOrFail();
In this case, it can be displayed directly in the view $person->name
, not the other way around $person[0]->name
.
You need @foreach.
Upstairs positive solution
@foreache($person as $temp) {$temp->foo}@endforeach
The data form you have queried can be tried
dd();
The data passed in Laravel is generally in the form of a JSON array
The same in learning laravel, if there are mistakes in the place welcome to point out