I have a view that displays a shopping cart in tabular form. Each shopping cart entry needs to display the details of the flowers currently purchased and the purchased quantity. In the database, there are two tables, flower and cart. The former contains The basic information of the flower, such as flower name, etc. The latter contains the information of the shopping cart entry, including user ID, flower ID, purchase quantity, etc. To obtain a user's shopping cart data, I need to connect the two tables and return the results of the connection to view
My project contains two Models, namely Cart (shopping cart entry) and Flower (flower). There is also a Controller, namely CartController, which contains a method to obtain a user's shopping cart data and pass it to the view. Methods.
The following are some of my ideas, but since I am new to Laravel, I am not sure about the feasibility and specific operations of implementing these ideas, so I hope you can give me some advice. The specific ideas are as follows:
My database contains a view of a user's shopping cart data, so I am considering reading the data directly from the view. I cannot find the query builder and Eloquent methods to operate on the view. So I considered using native sql
Query constructor multi-table query
Eloquent correlation. I have been using Eloquent before, so I want to continue using it, so I found out about the knowledge point of correlation. However, according to the documentation, I have doubts about whether it can perform multi-table queries
I have only been exposed to Laravel for a short time, and there may be many things that make people laugh in the above questions. If you have any suggestions for my questions, you are welcome to give them, thank you!
伊谢尔伦2017-05-18 10:49:22
I’ll write you a demo, you can just change it to what you want
DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.id', 'contacts.phone', 'orders.price')
->get();
The above is the join operation. The user table is connected to the contacts and orders tables, followed by the on condition, and the content of the select is the field to be checked.