簡體   English   中英

Laravel Eloquent不能使用不查詢關系的地方

[英]Laravel Eloquent can't use where not to query relationship

我的問題:我似乎無法使用Laravel的Eloquent ORM在關系集合上使用where('user_id', '<>', Auth::id()

在Laravel 5中,我有一個具有以下架構的數據庫:

| 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 />';
}

但是我只是不能讓count函數起作用。

如果在循環中將messages->更改為message()-> ,則可以重新查詢關系集。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM