[英]Unable to get data from two tables in laravel
I am doing project in laravel. 我在laravel做项目。
Database Table 1 数据库表1
user_verification
------------------------------------------
'id', 'user_id', 'verified', 'verified_by'
Database Table 2 数据库表2
user
-----------------------------
'id', 'name', 'email', 'pass'
I want to get data as all users which are verified('verified' = 1) and name of user('verified_by' is user id from user table) who does verification. 我希望获得数据,因为所有经过验证的用户('已验证'= 1)和用户名('verified_by'是用户表中的用户ID)都会进行验证。
My controller method is like, 我的控制器方法是这样的,
public function verifiedProviders(){
//$user_data = sql query goes here...
return view('display',compact('user_data '));
}
for example: , 例如:
I want to display in blade file as follow 我想在刀片文件中显示如下
name:abc,
email:abc@gmail.com,
verified by: name (id present in *verified_by* field) from *user* table.
I am not getting how to get and display this data in laravel's blade file. 我没有得到如何在laravel的刀片文件中获取和显示这些数据。 How to use relationships eloquent method or database query. 如何使用关系eloquent方法或数据库查询。 Thanks in advance. 提前致谢。
User::select('name', 'email', 'u2.name as verified_by_name')
->join('user_verification', 'user_verification.user_id', '=', 'user.id')
->join('user u2', 'user_verification.verified_by', '=', 'u2.id')
->all(); // I guess
Of course you may want to limit your query to return only the first 25 results and display them, etc. 当然,您可能希望将查询限制为仅返回前25个结果并显示它们等。
References: 参考文献:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.