Laravel5 in a many-to-many relationship, how to loop the output of one of the table's fields in the blade template, and output the field of another table whose association corresponds

Source: Internet
Author: User
As an example:
For both the project (projects) and student (students) tables, each student can have multiple projects, each of which can be done by multiple people, so add an associated table students_projects, and now I want to loop out all the students ' names and school numbers, And then you have to output the project that the student has done under each name number, and if you output a list of projects for a single student, I can get a list of the students ' projects in the controller in a similar $roles = User::find(1)->roles way, but for the information to loop out the students, as the relevant party documentation says. and output students corresponding to the projects, how should be implemented?

Reply content:

As an example:
For both the project (projects) and student (students) tables, each student can have multiple projects, each of which can be done by multiple people, so add an associated table students_projects, and now I want to loop out all the students ' names and school numbers, And then you have to output the project that the student has done under each name number, and if you output a list of projects for a single student, I can get a list of the students ' projects in the controller in a similar $roles = User::find(1)->roles way, but for the information to loop out the students, as the relevant party documentation says. and output students corresponding to the projects, how should be implemented?

Write directly in the template
@foreach ($students as $student)
@foreach ($student->projects as $project)
<项目> {{$project->xxx}}
@endforeach
@endforeach

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