laravel 查找数据 Eloquent ORM 查询
现在 有 2个 表
goods 和 products
<code> $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]; } </code>
首先 我们去 goods 表 去 查询 merchant_id = 5 的 商品;
因为 在 goods 表里面 没有 改 商品的 名字 和 更多详细的信息 ;
所以 需要 循环 goods 表里查询到的数据 通过 goods 表的 product_id 字段 去 products 查询 products 的 id 字段 所匹配的 更多信息 (name ) 等等
改怎么查询呢
我现在想到的 是 给 $products 这对象 循环添加查询到的 名字
有大神指导一下吗 (*^__^*) 嘻嘻……
回复内容:
现在 有 2个 表
goods 和 products
<code> $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]; } </code>
首先 我们去 goods 表 去 查询 merchant_id = 5 的 商品;
因为 在 goods 表里面 没有 改 商品的 名字 和 更多详细的信息 ;
所以 需要 循环 goods 表里查询到的数据 通过 goods 表的 product_id 字段 去 products 查询 products 的 id 字段 所匹配的 更多信息 (name ) 等等
改怎么查询呢
我现在想到的 是 给 $products 这对象 循环添加查询到的 名字
有大神指导一下吗 (*^__^*) 嘻嘻……
<code>php</code><code>class Goods extends Model { public function product() { return $this->belongsTo('App\Products'); } } $products = Goods::with('product')->where('merchant_id','=','5')->get();//数组里的product就是goods 表的 product_id 对应products的信息 </code>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

The main differences between Laravel and Yii are design concepts, functional characteristics and usage scenarios. 1.Laravel focuses on the simplicity and pleasure of development, and provides rich functions such as EloquentORM and Artisan tools, suitable for rapid development and beginners. 2.Yii emphasizes performance and efficiency, is suitable for high-load applications, and provides efficient ActiveRecord and cache systems, but has a steep learning curve.

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

The essential Laravel extension packages for 2024 include: 1. LaravelDebugbar, used to monitor and debug code; 2. LaravelTelescope, providing detailed application monitoring; 3. LaravelHorizon, managing Redis queue tasks. These expansion packs can improve development efficiency and application performance.

Integrating Sentry and Bugsnag in Laravel can improve application stability and performance. 1. Add SentrySDK in composer.json. 2. Add Sentry service provider in config/app.php. 3. Configure SentryDSN in the .env file. 4. Add Sentry error report in App\Exceptions\Handler.php. 5. Use Sentry to catch and report exceptions and add additional context information. 6. Add Bugsnag error report in App\Exceptions\Handler.php. 7. Use Bugsnag monitoring

Use the EXPLAIN command to analyze the execution plan of MySQL queries. 1. The EXPLAIN command displays the execution plan of the query to help find performance bottlenecks. 2. The execution plan includes fields such as id, select_type, table, type, possible_keys, key, key_len, ref, rows and Extra. 3. According to the execution plan, you can optimize queries by adding indexes, avoiding full table scans, optimizing JOIN operations, and using overlay indexes.
