简体   繁体   English

laravel查询构建器中的其他位置

[英]Additional where in laravel query builder

I have joined table in my query but i need to validate the value of column in that table, it returns 我在查询中加入了表格,但是我需要验证该表格中列的值,它返回

 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'on' in 'on clause'

Code

$interests = DB::table('projects')
->where('projects.published', 'y')
->whereDate('projects.created_at', Carbon::now()->subDay())
->join('project_tag', 'project_tag.project_id', '=', 'projects.id') //get project tags


->join('mailings', 'mailings.interests', '=', 'on') //get mailing lists with on value in interests column


->join('users', 'users.id', '=', 'mailings.user_id') //get mailing users
->join('user_interests', 'user_interests.user_id', '=', 'users.id') //get mailing users intrests (same as tags in projects)
->select('projects.*', 'users.*')
->get();

my issue is in this line 我的问题在这一行

->join('mailings', 'mailings.interests', '=', 'on')

I just want to join mailings where their intrests column value is set to on 我只想加入其intrests列值设置为on mailings

Any idea? 任何想法?

Update 更新

I changed my issued line to: 我将发布行更改为:

->join('mailings', function ($join) {
  $join->where('mailings.interests', '=', 'on');
})

now it's returning results but output data are duplicated and not complete. 现在它正在返回结果,但是输出数据重复且不完整。 here is what i get on dd 这是我在dd得到的

lluminate\Support\Collection {#819     
 #items: array:6 [                     
   0 => {#824                          
     +"title": "test 1"                
     +"slug": "test-1"                 
     +"budget": 23454235.0             
     +"name": "Diana Bosco"            
     +"email": "arunolfsson@example.net
   }                                   
   1 => {#827                          
     +"title": "test 1"                
     +"slug": "test-1"                 
     +"budget": 23454235.0             
     +"name": "Diana Bosco"            
     +"email": "arunolfsson@example.net
   }                                   
   2 => {#820                          
     +"title": "test 1"                
     +"slug": "test-1"                 
     +"budget": 23454235.0             
     +"name": "admin"                 
     +"email": "admin@admin.com"       
   }                                   
   3 => {#826                          
     +"title": "test 1"                
     +"slug": "test-1"                 
     +"budget": 23454235.0             
     +"name": "admin"                 
     +"email": "admin@admin.com"       
   }                                   
   4 => {#823                          
     +"title": "test 1"                
     +"slug": "test-1"                 
     +"budget": 23454235.0             
     +"name": "Aurelia Treutel I"      
     +"email": "korey13@example.net"   
   }                                   
   5 => {#825                          
     +"title": "test 1"                
     +"slug": "test-1"                 
     +"budget": 23454235.0             
     +"name": "Aurelia Treutel I"      
     +"email": "korey13@example.net"   
   }                                   
 ]

issues 问题

  1. Data are duplicated for each user 为每个用户复制数据
  2. It gets same project for all users while it should be different based on their tags. 它为所有用户提供了相同的项目,但根据他们的标签应该有所不同。

To be clear Logic 要明确 Logic

  1. I my users have tags (i called it interests) 我的用户有标签(我称之为兴趣)
  2. Projects have tags as well 项目也有标签
  3. I want to get projects that have same tags as user interests and show them to user. 我想获得与用户兴趣具有相同标签的项目,并将其显示给用户。
  4. List item 项目清单

Use ->join() to join the mailings table via FK reference, same as you have done for the other tables, then change the where clause to an array of conditions: 使用->join()通过FK引用加入mailings表,就像您对其他表所做的一样,然后将where子句更改为条件数组:

->where([
    ['projects.published', '=', 'y'],
    ['mailings.interests', '=', 'on'],
])

Apply the filter as, 将过滤器应用为

$interests = DB::table('projects')
            ->where('projects.published', 'y')
            ->whereDate('projects.created_at', Carbon::now()->subDay())
            ->join('project_tag', 'project_tag.project_id', '=', 'projects.id') //get project tags

            // the filter
            ->where('mailings.interests', 'on')

            ->join('users', 'users.id', '=', 'mailings.user_id') //get mailing users
            ->join('user_interests', 'user_interests.user_id', '=', 'users.id') //get mailing users intrests (same as tags in projects)
            ->select('projects.*', 'users.*')
            ->get();

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

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