如何在Laravel中联接两个不同的表 [英] How to Join two different tables in Laravel
问题描述
Connection.php第729行中的QueryException:SQLSTATE [42S22]:找不到列:1054中的未知列'site_name''where子句'(SQL:从中选择
电子邮件日期
,url
,收件人
report_list
,其中site_name
= mywebsite)
QueryException in Connection.php line 729: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'site_name' in 'where clause' (SQL: select
email_date
,url
,recipient
fromreport_list
wheresite_name
= mywebsite)
$records = DB::table('report_list')
->select('email_date','url','recipient')
->where('site_name',$site_name)
->get();
return records;
return view('monthlyReport')
->with('records',$records)
->with('site_name',$site_name);
我的 site_name
在不同的表上,我不知道是否需要为这两个应用程序加入Join或制作模型.
My site_name
was on different table and I don't know if I need to put Join or Make a model for this two.
有人可以帮助我进行此查询吗?
Can someone help me with this query?
推荐答案
- 首先,您需要在数据库的"report_list"表中添加名为"site_name"的列.
-
此查询供您连接2个表(这里我以示例"users"表作为第二个表,如果第二个表不同,请使用您的表)->
- First of all You need to add column named "site_name" to your "report_list" table in database.
this query is for you to join 2 tables (here I took example "users" table as second table If your second table is defferent use your) ->
$records = DB::table('report_list')
->join('users', 'report_list.user_id', '=', 'users.id')
->where('report_list.site_name', '=', $site_name);
->select('users.*', 'report_list.email_date','report_list.url','report_list.recipient')
->get();
return view('monthlyReport')
->with(['records' => $records , 'site_name' => $site_name ]);
这篇关于如何在Laravel中联接两个不同的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!