2013-12-11 61 views
1

我試圖建立一個消息系統。我有哪些用戶和對話有多對多的關係。Laravel 4多對多關係錯誤,當沒有記錄匹配

用戶模式:

public function conversations() { 
    return $this->belongsToMany('Conversation'); 
} 

對話

public function users() { 
    return $this->belongsToMany('User'); 
} 

我能得到用戶和通話用戶與:

$user = User::with('conversations')->find(Auth::user()->id); 

或者:

$user = User::with('conversations')->find(1); 

的問題是,當用戶沒有任何交談,我得到一個例外:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error 
in your SQL syntax; check the manual that corresponds to your MySQL server 
version for the right syntax to use near ')' at line 1 (SQL: select * from 
`conversations` where `id` in()) (Bindings: array ()) 

如何測試用戶是否有對話,以避免這種情況?提前致謝。

編輯:

我創建的ConversationsController新的對話,像這樣:

更新後,大衛·巴克的解決方案的創建方法:

public function create($id) 
{ 
    // Check user exists 
    $user = User::find($id); 
    if (! $user) 
     throw new Exception('User not found'); 

    // Create a new conversation 
    $conversation = new Conversation(); 
    $conversation->creator = Auth::user()->id; 
    $conversation->save(); 

    // Attach the users to the conversation 
    $conversation->users()->attach($user); 
    $conversation->users()->attach(Auth::user()); 

    return $conversation; 
} 

但我仍然得到例外這種方式也。

從幾個小時我覺得這可能是一個可能的解決方案的項目走開後:

$con = User::find(Auth::user()->id)->conversations; 

if(0 < $con->count()) { 

    $conversationIds = array(); 
    foreach ($con as $conversation) { 
     $conversationIds[] = $conversation->id; 
    } 

    // I THINK THIS WAS ACTUALLY THE PROBLEM 
    $conversations = Conversation::with('users')->whereIn('id', $conversationIds)->get(); 

} else { 
    $conversations = false; 
} 
+0

您可以發佈您在模型中的關係嗎? –

+0

@DavidBarker我編輯將它包含在頂部 –

+0

你有一個數據透視表'conversations_users'? –

回答

1

在你的類用戶,添加您的關係:

class User extends Eloquent implements UserInterface, RemindableInterface { 

    function conversations(){ 
     return $this->hasMany('Conversation'); 
     //assuming you have a conversation model too 
    } 

} 

,那麼你可以這樣做而不是:

$conversations = Auth::user()->conversations; 

或查找特定ID:

$conversations = User::find($id)->conversations; 

,你必須附加到當前用戶的所有對話..
你已經拿到了交談後..你可以檢查它是否在做任何事情之前空..
(例如:if(0 < $conversations->count()){ }

你可以在這裏找到更多的信息對laravel docs


UPDATE:

你可以試試這個嗎?

$user = User::has('conversation')->where('user.id','=', $id)->get(); 

更新2:

// get all the conversations connected to the current user 
$my_conversations = Auth::user()->conversations; 

// generate a list of all the conversation ids 
foreach($my_conversations as $my_conversation) { 
    $conversation_ids[] = $my_conversation->id; 
} 

if(!empty($conversation_ids)) { 
    // get all the users for each conversations, via eager loading 
    $conversations = Conversation::with('User') 
     ->whereIn('id', $conversation_ids)->get(); 
} 
+1

'User :: with('conversations')'已經假定模型中有'conversations()'方法,並且OP聲明它作品。這不是一個解決方案。 –

+1

「問題在於,當用戶沒有任何對話時」 - 這就是爲什麼您需要首先獲得對話並檢查是否存在該用戶的任何對話。 – reikyoushin

+0

和用戶明確說過「如何測試if用戶有對話來避免這種情況?提前致謝。「,現在有什麼問題? – reikyoushin

1

此刻你是不是讓雄辯管理整個關係數據的插入。雄辯地管理這件事非常容易,最好讓它爲你做這件事,這看起來可能是你的問題的根源。

public function create($id) 
{ 
    // Use the logged in user or the passed in user ID for user object 
    $user = isset($id) ? User::find($id) : Auth::user(); 

    // Check user exists 
    if (! $user) 
     throw new Exception('User not found'); 

    // Create a new conversation 
    $conversation = new Conversation(array(
     'creator' => $user->id 
    )); 

    $conversation->save(); 

    // Attach the user model to the conversation 
    // this method will insert all of the relational 
    // information into the pivot table for you 
    $conversation->users()->attach($user); 

    return $conversation; 
} 
+0

我可以看到這是一個更好的方法來做到這一點,而且這種方法也能正常工作,但不幸的是這並不能解決問題。我仍然得到相同的例外。到目前爲止,我只能通過if(0 <$ conversations-> count()){})解決方案來避免它,但我想知道它爲什麼會拋出異常,而不僅僅返回null。 –

+0

我剛剛在類似的代碼上運行測試,我只是在模型上獲得空關係,無論我是否渴望加載,都不會拋出異常。讓我研究一下,如果你正確地使用雄辯,你不應該得到那個例外。 –

+0

我在問題的底部添加了新的創建方法。 –