A more complex laravel orm eloquent query

Source: Internet
Author: User
The table structure is as follows:

表名:projects字段:id, name表名:projects_services(1 project_id <-> M service_id)字段:project_id, service_id表名:services字段:id, name表名:services_modules(M service_id <-> N module_id)字段:service_id, module_id表名:modules字段:id, name表名:modules_scripts(1 module_id<->1 script_id)字段:module_id, script_id表名:scripts字段:id, name

If I want to use Laravel's eloquent query the following information, how should it be implemented?

script_id, Script_name, Module_name, service_name, project_name

Reply content:

The table structure is as follows:

表名:projects字段:id, name表名:projects_services(1 project_id <-> M service_id)字段:project_id, service_id表名:services字段:id, name表名:services_modules(M service_id <-> N module_id)字段:service_id, module_id表名:modules字段:id, name表名:modules_scripts(1 module_id<->1 script_id)字段:module_id, script_id表名:scripts字段:id, name

If I want to use Laravel's eloquent query the following information, how should it be implemented?

script_id, Script_name, Module_name, service_name, project_name

Can be achieved, and laravel will achieve very elegant, you are here
projects->project_services->services->services_modules->modules->modules_scripts->scriputs, seven table joint query, you first want to create these seven models, and to define the respective relationships, and then: $list =Projects::with('project_services.services.services_modules.modules.modules_scripts.scripts')->where('写上你的条件')-first(); 只需要这么简短的一句代码,就可以实现你的需求,就能非常方便的获取你所要的数据 project_name:$list->name, service_name:$list->project_services->services->name; module_name:$list->project_services->services->services_modules->modules->name; sript_name:$list->project_services->services->services_modules->modules->modules_scripts->scripts->name; sript_id:$list->project_services->services->services_modules->modules->modules_scripts->scripts->id; This method I use more, very convenient, but like such a long connection query, I have not used.

The first floor has been said to be good

First define the relationships of these tables in Laravel eloquent, document address: Http://laravel.com/docs/5.1/eloquent-relationships

You can then use the Projects::with (...) directly in the controller. The content you need) is ready.

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