Ask laravelview how to display the object values passed in the controller.

Source: Internet
Author: User
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.foreachNow. 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->nameInstead$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:

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.