在Laravel 5中订购 [英] OrderBy in Laravel 5

查看:39
本文介绍了在Laravel 5中订购的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Laravel 5创建一个基本的twitter-eqs消息传递应用程序.

I’m trying to make a a basic twitter-eqs messaging app with Laravel 5.

有消息和发件人.我正在尝试编写一个查询,以显示最新消息,发件人以及发件人显示的消息总数.该查询应仅显示每个用户的最新消息.例如如果一个人在最近5分钟内发送了6条消息,则只会显示其中的最新消息.

There are messages and senders. I’m trying to write a query which displays latest messages, the sender, and the total number of messages shown by the sender. The query should only show the latest message for each user. e.g. if one person has sent 6 messages in the last 5 mins, it’d only show the latest one from them.

我要以口才的方式做的是:

What I'm trying to do in an Eloquent way is:

  1. 查询最新消息,GroupBy发件人
  2. 选择发件人姓名,将发件人姓名加入查询1
  3. 查询邮件的数量GroupBender,加入查询2

此过程可能从根本上是错误的.

This process may be fundamentally wrong.

然后,在我看来,我将渲染:发件人A-(31条消息),您好,这是我的消息.

Then, in my view, I'd render: Sender A - (31 messages), Hello this is my message.

表格

我有2张桌子

发件人

ID |名称

消息

ID |SenderID |留言|Created_at

ID | SenderID | Message | Created_at

查询

$latestMessages = Sender::
join('messages', 'messages.sender_id', '=', 'senders.id')
->OrderBy('messages.created_at', 'asc')
->GroupBy('senders.id')
->get();

问题

这将输出OLDEST消息,而不是NEWEST消息.(而且,我不知道如何计算来自每个发件人的邮件并加入该邮件).

This outputs the OLDEST message, not the NEWEST. (also, I can't figure out how to count the messages from each sender and join this).

我尝试过更改"ASC"并"DESC"无济于事.当我删除GroupBy时,它会确实以所需的顺序显示消息(从最新到最旧),但是我不知道如何只显示每个发件人1个消息.

I've tried changing the "ASC" and to "DESC" to no avail. When I remove the GroupBy, it does show the messages in the desired order (newest to oldest) but I can't work out how to only show 1 per sender.

问题有人可以解释一下如何使用Laravel 5来实现这一目标,或者我的工作原理以及我应该做什么吗?

QUESTION Could somebody explain how to achieve this with Laravel 5, or the floor in my approach and what I should look to do?

如果您可以解释如何在Eloquent中查询和加入count(),那就太不可思议了.

If you can explain how query and join the count() in Eloquent, that'd be incredible.

推荐答案

您可以使用以下方法实现此目的:

You can achieve this with following:

  1. 创建和执行发件人类
  2. 创建并实现Message类

发件人模型:

namespace App;
use Illuminate\Database\Eloquent\Model;

class Sender extends Model {

    public function messages() {
        return $this->hasMany('App\Message');
    }

}

消息模型:

namespace App;
use Illuminate\Database\Eloquent\Model;

class Message extends Model { 

    public function sender() {
        return $this->belongsTo('App\Sender');
    }

    // Create scope http://laravel.com/docs/5.0/eloquent#query-scopes
    public function scopeLatest($query) {
        return $query->orderBy('created_at', 'asc');
    }
}

因此,现在您可以获取所有发件人,遍历所有发件人,并显示其最新消息和总计数,如下所示:

So now you can get all Senders, loop throught them and display their latest message and total count like this:

$senders = Sender::with('messages')->all();

foreach ($senders as $sender) {
    echo "Total Messages: " . $sender->messages->count();
    echo "<br />";
    echo "Latest message: " . $sender->messages()->latest()->first()->Message;
}

这篇关于在Laravel 5中订购的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆