Heim  >  Artikel  >  Backend-Entwicklung  >  laravel 查找数据 Eloquent ORM 查询

laravel 查找数据 Eloquent ORM 查询

WBOY
WBOYOriginal
2016-06-06 20:31:591077Durchsuche

现在 有 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>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn