Laravel query data EloquentORM Query

Source: Internet
Author: User
There are now 2 tables goods and products {code ...} first, go to the goods table to query the merchant_id5 product. Because the product name and more detailed information are not changed in the goods table, we need to cycle the data queried in the goods table... there are now 2 tables
Goods and products

 $products = Goods::where('merchant_id','=','5')->get(); foreach($products as $p){            print_r($p->product_id);            $_name = Product::where('id','=',$p->product_id)->get();            $_name = $_name[0];}

First, go to the goods table to query the products with merchant_id = 5;

Because there is no change to the product name and more detailed information in the goods table;

Therefore, you need to use the product_id field of the goods table to query more information (name) matched by the id field of products in the goods table.

How can I query it?

Now I want to add the queried name to the $ products object loop.

Can you give me some guidance (* ^__ ^ ......

Reply content:

There are now 2 tables
Goods and products

 $products = Goods::where('merchant_id','=','5')->get(); foreach($products as $p){            print_r($p->product_id);            $_name = Product::where('id','=',$p->product_id)->get();            $_name = $_name[0];}

First, go to the goods table to query the products with merchant_id = 5;

Because there is no change to the product name and more detailed information in the goods table;

Therefore, you need to use the product_id field of the goods table to query more information (name) matched by the id field of products in the goods table.

How can I query it?

Now I want to add the queried name to the $ products object loop.

Can you give me some guidance (* ^__ ^ ......

phpClass Goods extends Model {public function product () {return $ this-> belonsto ('app \ products') ;}}$ Products = Goods: with ('product ') -> where ('Merchant _ id', '=', '5')-> get (); // the product in the array is the product_id information of the goods table corresponding to products.

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.