laravel 查找数据 Eloquent ORM 查询
Jun 06, 2016 pm 08:31 PM
现在 有 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>

Heißer Artikel

Hot-Tools-Tags

Heißer Artikel

Heiße Artikel -Tags

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

PHP 8.4 Installations- und Upgrade-Anleitung für Ubuntu und Debian

So beheben Sie den Fehler „mysql_native_password nicht geladen' unter MySQL 8.4
