doushi5913
2015-04-27 09:27
浏览 77
已采纳

Laravel Eloquent不能使用哪里不查询关系

My problem: I can't seem to use where('user_id', '<>', Auth::id() on a relationship collection using Laravel's Eloquent ORM.

In Laravel 5, I have a database with the following schema:

| USERS    | JOBS    | CONVERSATIONS | MESSAGES        | CONVERSATION_USER |
| id       | id      | id            | id              | conversation_id   |
| username | user_id | job_id        | conversation_id | user_id           |
| password | title   |               | user_id         |                   |
|          |         |               | message         |                   |

and with the following relationships:

User model:
$jobs = $this->hasMany('job');
$messages = $this->hasMany('message');
$conversations = $this->belongsToMany('conversation');

Job model:
$user = $this->belongsTo('user');
$conversations = $this->hasMany('conversation');
$messages = $this->hasManyThrough('message', 'conversation');

Conversation model:
$job = $this->belongsTo('job');
$messages = $this->hasMany('message');
$user = $this->belongsToMany('user');

Message model:
$user = $this->belongsTo('user');
$conversation = $this->belongsTo('conversation');

I'm trying to get the number of messages for a job, which have not been posted by the authenticated user. My code is:

// Load all of the jobs which relate to the logged in user
$jobs = Job::with(['messages'])->where('user_id', Auth::id())->OrderBy('id', 'DESC')->get();

foreach ($jobs as $job)
{
    // Count all messages that have not been sent by the logged in user
    echo $job->messages->where('user_id', '<>', Auth::id())->count().'<br />';
}

However I just cannot get the count function to work.

图片转代码服务由CSDN问答提供 功能建议

我的问题:我似乎无法使用 where('user_id 使用Laravel的Eloquent ORM在关系集合上使用','&lt;&gt;',Auth :: id()

在Laravel 5中,我有一个包含以下内容的数据库 schema:

  | USERS | JOBS | CONVERSATIONS | MESSAGES | CONVERSATION_USER | 
 | id | id | id | id | conversation_id | 
 | username | user_id | job_id | conversation_id  | user_id | 
 |密码|标题| | user_id | | 
 | | | |消息| | 
   
 
 

并具有以下关系:

 用户模型:
 $ jobs = $ this-&gt; hasMany('job'); 
 $ messages = $ this-&gt; hasMany('message'); 
 $  conversations = $ this-&gt; belongsToMany('conversation'); 
 
Job model:
 $ user = $ this-&gt; belongsTo('user'); 
 $ conversations = $ this-&gt; hasMany  ('对话'); 
 $ messages = $ this-&gt; hasManyThrough('message','conversation'); 
 
会话模型:
 $ job = $ this-&gt; belongsTo('job');  
 $ messages = $ this-&gt; hasMany('message'); 
 $ user = $ this-&gt; belongsToMany('user'); 
 
消息模型:
 $ user = $ this-&gt;  belongsTo('user'); 
 $ conversation = $ this-&gt; belongsTo('conversation'); 
   
 
 

我正在尝试获取的数量 作业的消息,尚未由经过身份验证的用户发布。 我的代码是:

  //加载与登录用户相关的所有作业
 $ jobs = Job :: with(['messages']) - &gt  ; where('user_id',Auth :: id()) - &gt; OrderBy('id','DESC') - &gt; get(); 
 
foreach($ jobs as $ job)
 {
  //计算登录用户尚未发送的所有消息
 echo $ job-&gt; messages-&gt; where('user_id','&lt;&gt;',Auth :: id()) - &gt;  count()。'&lt; br /&gt;'; 
} 
   
 
 

但是我无法使计数功能起作用。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doudu9094 2015-04-28 09:41
    已采纳

    If you change messages-> to message()-> within your loop, you will be able to re-query your relation set.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题