如何在laravel php内连接表

时间:2017-01-24 10:30:34

标签: php mysql laravel laravel-5.3

我有这个查询

SELECT * FROM books b 
INNER JOIN categories c ON b.category_id = c.id 
WHERE c.category_name = 'Comics'

因此通过参数

传递
SELECT * FROM books b 
INNER JOIN categories c ON b.category_id = c.id 
WHERE c.category_name = ?

我在Laravel中试过这样的

writer::join('categories','categories.id','=','writers.category_id')
  ->where('categories.category_name','Comics')
  ->get();

并带参数

writer::join('categories','categories.id','=','writers.category_id')
  ->where(['categories.category_name' => $cat])
  ->get();

我没有在Laravel中获得任何数据!我有什么错误吗?

1 个答案:

答案 0 :(得分:1)

请改为尝试:

writer::join('categories','categories.id','=','writers.category_id')
->where([
  ['categories.category_name', '=', $cat]
])->get();

writer::join('categories','categories.id','=','writers.category_id')
->where('categories.category_name', $cat)
->get();