繁体   English   中英

laravel MySQL查询联接两个表

[英]laravel mysql query joining two tables

我有两张桌子

--------------
Categories
--------------
id
category

--------------
Products
--------------
id
category
product details

我需要同时加入这两个类别并根据category id获取product details ,请帮助在laravel中建立查询

我试过了但是没用

$shares = \DB::table('products')
        ->join('categories', 'categories.category', '=', 'products.category')
        ->select('*')
        ->where('categories.id', '=', $id)
        ->get();

我得到一个错误是

无法将类型为stdClass的对象用作数组

尝试这个,

$shares = \App\Product::select('*')
        ->join('Category', function ($join) {
            $join->on('categories.category', '=','products.category');
        })->where('categories.id',$id)
        ->get();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM