Laravel 使用别名加入的查询生成器
Laravel Query Builder joining with alias
我有两个table
未知table
id | parent_id
客户table
id | parent_id
我的代码:
$transaction = DB::table($name.'_transactions')
->where('user_id', $id)
->join('users', 'users.id' , '=' , 'parent_id')
->get();
加入 clients.id
=> unknown.parent_id
...因为客户 table 有一个 parent_id
这就是为什么我必须使用别名
请帮助我这样做!
使用别名工作解决方案:
$tablename = Roles::where('id', '=' , 6)->first();
$name = str_replace(' ', '_', trim($tablename->name));
$tb = $name.'_transactions';
$exist_in_db = DB::table($tb)
->where('user_id', $id)
->get();
if(count($exist_in_db) > 0) {
$transaction = DB::table("$tb as newtable")
->where('user_id', $id)
->join('users', 'users.id' , '=' , 'newtable.parent_id')
->get();
我有两个table
未知table
id | parent_id
客户table
id | parent_id
我的代码:
$transaction = DB::table($name.'_transactions')
->where('user_id', $id)
->join('users', 'users.id' , '=' , 'parent_id')
->get();
加入 clients.id
=> unknown.parent_id
...因为客户 table 有一个 parent_id
这就是为什么我必须使用别名
请帮助我这样做!
使用别名工作解决方案:
$tablename = Roles::where('id', '=' , 6)->first();
$name = str_replace(' ', '_', trim($tablename->name));
$tb = $name.'_transactions';
$exist_in_db = DB::table($tb)
->where('user_id', $id)
->get();
if(count($exist_in_db) > 0) {
$transaction = DB::table("$tb as newtable")
->where('user_id', $id)
->join('users', 'users.id' , '=' , 'newtable.parent_id')
->get();