Php Laravel Eloquent无法使用where not查询关系
我的问题是:我似乎无法使用Laravel雄辩的ORM在关系集合上使用where'user_id',Auth::id 在Laravel 5中,我有一个具有以下模式的数据库:Php Laravel Eloquent无法使用where not查询关系,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我的问题是:我似乎无法使用Laravel雄辩的ORM在关系集合上使用where'user_id',Auth::id 在Laravel 5中,我有一个具有以下模式的数据库: | USERS | JOBS | CONVERSATIONS | MESSAGES | CONVERSATION_USER | | id | id | id | id | conversation_id | | username
| 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 | |
并具有以下关系:
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');
我正在尝试获取作业的消息数,这些消息尚未由经过身份验证的用户发布。我的代码是:
// 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 />';
}
但是,我无法让计数函数工作。如果在循环中将messages->更改为message->,则可以重新查询关系集 尝试将循环中的消息->更改为消息->。@MattBurrow您需要将您的评论作为有效的答案!答案已添加。