Laravel 根据查询创建不同的视图

Laravel creating different views from query

我有以下查询:

    $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();

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

@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

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

@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

所以它列出了第一个标签,包括所有具有该特定标签的约会,然后是下一个带有约会的标签,依此类推。我的上述查询是否可以简单地做到这一点?或者我应该在我的模型中创建关系并从那里开始工作?当然,如果有更好的方法来完成这一切,我会洗耳恭听。

下面是我的数据库表的导出,以便更好地理解我的代码。

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 collections groupBy:

将其导入您的 header:

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