Ask Laravel view how to display the object values passed in the controller.

Source: Internet
Author: User
Tags compact
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

  • 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.