我想建立一個使用Laravel 5和使用雄辯ORM工作網站的消息傳遞系統。基本前提是有人發佈了一份工作,人們可以通過消息迴應該工作。 MySQL數據庫的結構爲這樣:Laravel雄辯的關係和連接多個表
**users table**
id
username
password
**jobs table**
id
user_id (FK with id on Users table)
slug
title
description
**conversations table**
id
job_id (FK with id on Jobs table)
**messages table**
id
conversation_id (FK with conversations table)
user_id (FK with id on users table)
message
last_read
**conversation_user table**
conversation_id (FK with id on Conversation table)
user_id (FK with id on Users table)
當用戶發現自己喜歡的工作,他們可以將消息發送到創造就業,這將反過來創建一個新的對話。然後使用新創建的對話ID傳遞給消息表(與消息文本本身一起),然後使用對話ID以及參與對話的兩個用戶(即發佈的人)更新conversation_user數據透視表作業併發送消息的人)
我對每個表的模型和關係的總結是:
**Job.php**
HasMany - Conversation model
BelongsTo - User model
**Conversation.php**
BelongsTo - Job model
HasMany - Message model
BelongsToMany - User model
**Message.php**
BelongsTo - Conversation model
BelongsTo - User model
**User.php**
HasMany - Job model
HasMany - Message model
BelongsToMany - Conversation model
我已經安裝在Conversation.php查詢範圍(我的口才模型對話表),其完成顯示經過驗證的用戶參與的會話的任務:
public function scopeParticipatingIn($query, $id)
{
return $query->join('conversation_user', 'conversations.id', '=', 'conversation_user.conversation_id')
->where('conversation_user.user_id', $id)
->where('conversation_user.deleted_at', null)
->select('conversations.*')
->latest('updated_at');
}
和通過我的對話信息庫,我通過對查詢範圍的結果,我認爲在我MessagesController像這樣:
public function __construct(ConversationInterface $conversation)
{
$this->middleware('auth');
$this->conversation = $conversation;
}
public function index()
{
$currentUser = Auth::id();
$usersConversations = $this->conversation->ParticipatingIn($currentUser, 10);
return view('messages.index')->with([
'usersConversations' => $usersConversations
]);
}
和用於參考的ConversationInterface爲界到我ConversationsRepo:
public $conversation;
private $message;
function __construct(Model $conversation, MessageInterface $message)
{
$this->conversation = $conversation;
$this->message = $message;
}
public function participatingIn($id, $paginate)
{
return $this->conversation->ParticipatingIn($id)->paginate($paginate);
}
我的問題是,鑑於我擁有我認爲正確的關係,我如何從對話表的job_id中傳遞特定作業的標題以及最後一封消息的前幾個單詞那是在對話中發出的?