Laravel从查询创建不同的视图 [英] Laravel creating different views from query

查看:225
本文介绍了Laravel从查询创建不同的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询:

    $appointments = Appointment::orderBy('appointment', 'asc')
        ->leftJoin('appointments_labels', 'appointments_labels.id', '=', 'appointments.label_id')
        ->leftJoin('appointments_statuses', 'appointments_statuses.id', '=', 'appointments.status_id')
        ->select('appointments.id', 'appointments.appointment', 'appointments.label_id', 'appointments.status_id', 'appointments_labels.label', 'appointments_statuses.status')
        ->get();

我使用它的目的基本上是像这样显示我所有的约会(我的视图的简化版本):

What I am using this for is basically to display all my appointments like so (simplistic version of my view):

@foreach($appointments as $appointment)
    <table>
        <tr>
            <th>Appointment Name</th>
            <th>Label</th>
            <th>Status</th>
        </tr>

        <tr>
            <td>{{ $appointment->appointment }}</td>
            <td>{{ $appointment->label }}</td>
            <td>{{ $appointment->status }}</td>
        </tr>
    </table>
@endforeach

这将显示我的所有约会,包括来自正确数据库表的正确标签名称和状态名称.我希望能够创建如下视图:

This will display all my appointments, including the correct label name and status name from the proper database tables. My wish is, to be able to create a view like so:

@foreach($appointments as $appointment)
    <table>
        <tr>
            <th>{{ $appointment->label }}</th>
            <th>Status</th>
        </tr>

    @foreach($appointment->appointments as $label)

        <tr>
            <td>{{ $label->appointment }}</td>
            <td>{{ $label->status }}</td>
        </tr>

    @endforeach

    </table>
@endforeach

因此,它列出了第一个标签,其中包含具有该特定标签的所有约会,然后列出包含该约会的下一个标签,依此类推.我上面的查询有可能吗?还是应该在模型中创建关系并从那里开始工作?当然,如果有更好的方法来完成所有这些工作,那么我会耳熟能详.

So it lists the first label inluding all appointments which have that particular label, and then the next label with it's appointments, and so on. Is that simply possible with my above query? Or should I create relations in my models instead and work from there? If there is a better way to do all this ofcourse, I'm all ears.

下面是我的数据库表的导出,可能是为了更好地理解我的代码.

Below is an export of my database tables for a better understanding of my code perhaps.

CREATE TABLE IF NOT EXISTS `appointments` (
  `id` int(11) NOT NULL,
  `appointment` varchar(255) NOT NULL,
  `location` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `start` datetime NOT NULL,
  `end` datetime NOT NULL,
  `label_id` int(11) NOT NULL,
  `status_id` int(11) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
)

CREATE TABLE IF NOT EXISTS `appointments_labels` (
  `id` int(11) NOT NULL,
  `label` varchar(255) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
)


CREATE TABLE IF NOT EXISTS `appointments_statuses` (
  `id` int(11) NOT NULL,
  `status` varchar(255) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
)

示例:

推荐答案

您可以使用laravel集合groupBy:

You can use laravel collections groupBy:

将其导入标题中

use Illuminate\Support\Collection;

然后:

$appointments = Appointment::orderBy('appointment', 'asc')
    ->leftJoin('appointments_labels', 'appointments_labels.id', '=', 'appointments.label_id')
    ->leftJoin('appointments_statuses', 'appointments_statuses.id', '=', 'appointments.status_id')
    ->select('appointments.id', 'appointments.appointment', 'appointments.label_id', 'appointments.status_id', 'appointments_labels.label', 'appointments_statuses.status')
    ->get();

$appointments = Collection::make($appointments)->groupBy('label');
var_dump($appointments);

这篇关于Laravel从查询创建不同的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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