简体   繁体   English

Laravel雄辩的关系和连接多个表

[英]Laravel Eloquent relationships and connecting multiple tables

I am trying to build a messaging system for a job site using Laravel 5 and using the Eloquent ORM. 我正在尝试使用Laravel 5和Eloquent ORM为工作站点构建消息传递系统。 The basic premise is that someone posts up a job, and people can respond to that job via a message. 基本前提是某人发布了一份工作,并且人们可以通过消息来响应该工作。 The MySQL database is structured as so: MySQL数据库的结构如下:

**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)

When a user finds a job they like, they can send a message to the job creator which will in turn create a new conversation. 当用户找到自己喜欢的工作时,他们可以将消息发送给工作创建者,后者随后将创建新的对话。 The newly created conversation id is then used passed to the messages table (alongside the message text itself) and then the conversation_user pivot table is updated with the conversation id as well as the two users who are participating in the conversation (ie the person who posted the job and the person sending the message) 然后使用新创建的对话ID传递到消息表(与消息文本本身一起),然后使用对话ID以及参与对话的两个用户(即发布者)更新对话用户透视表工作和发送消息的人)

I have a model for each table and a summary of the relationships are: 我为每个表都有一个模型,并且关系的摘要是:

**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

I have setup a query scope in Conversation.php (my Eloquent model for the Conversations table) which accomplishes the task of displaying the conversations that the authenticated user is participating in: 我在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');
}

and via my Conversations Repository, I pass on the results of the query scope to my view in my MessagesController like so: 并通过会话存储库,将查询范围的结果传递给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
    ]);
}

and for reference the ConversationInterface is bounded to my ConversationsRepo: 作为参考,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);
}

My question is, given I have what I believe are the right relationships, how I can pass the title of the specific job from the job_id on the conversations table and also the first few words of the last message that was sent within the conversation? 我的问题是,鉴于我有正确的关系,我该如何从对话表上的job_id传递特定职位的标题,以及对话中发送的最后一条消息的前几个单词呢?

I'm sorry if I'm stating the obvious, but: 很抱歉,如果我要说的很明显,但是:

Conversation model belongs to Job Model. 会话模型属于作业模型。 As you already has the conversation object/id, just do this: 由于您已经有了对话对象/ ID,只需执行以下操作:

//Controller
$conversation = App\Conversation::find($id);
return view('your view', compact('conversation'));

//View
$conversation->job->title; //the conversation belongs to a job, so requesting the job will return an instance of the job model, which can be asked for the title.

You can also use this on the view to get the first chars from the message: 您还可以在视图上使用它来从消息中获取第一个字符:

substr($conversation->messages->last()->message,0,desired lenght);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM