繁体   English   中英

laravel 关系票、票消息及消息附件

[英]laravel relationship tickets, ticket messages and message attachments

我在laravel写一个support ticket系统,需要建立表之间的关系。 我是 Laravel 的新手。我需要获取票证消息和这些消息的文件。 通过阅读文档,我已经在属于票证的消息之间建立了关系并且它可以正常工作。 但是,我需要获取这些消息下的文件,你能帮我吗?

消息的json如下:

{
  "ticket": [
    {
      "id": 1,
      "subject": "test 1",
      "department_id": 1,
      "priority": "low",
      "status": "open",
      "users_id": 1,
      "created_at": "2022-10-03T11:47:42.000000Z",
      "updated_at": "2022-10-04T17:31:10.000000Z",
      "deleted_at": null,
      "author": "Admin Admin",
      "author_id": 1,
      "department": "Genel",
      "messages": [
        {
          "id": 1,
          "admin_tickets_id": 1,
          "users_id": 1,
          "admin_users_id": null,
          "reply": "test mesajıdır.",
          "deleted_at": null,
          "created_at": "2022-10-03T11:47:42.000000Z",
          "updated_at": "2022-10-03T11:47:42.000000Z"
        },
        {
          "id": 5,
          "admin_tickets_id": 1,
          "users_id": null,
          "admin_users_id": 1,
          "reply": "test mesajıdır.",
          "deleted_at": null,
          "created_at": "2022-10-03T11:47:42.000000Z",
          "updated_at": "2022-10-03T11:47:42.000000Z"
        },
        {
          "id": 9,
          "admin_tickets_id": 1,
          "users_id": null,
          "admin_users_id": 1,
          "reply": "test için yaziyom",
          "deleted_at": null,
          "created_at": "2022-10-04T22:19:32.000000Z",
          "updated_at": "2022-10-04T22:19:32.000000Z"
        },
        {
          "id": 10,
          "admin_tickets_id": 1,
          "users_id": null,
          "admin_users_id": 1,
          "reply": "test",
          "deleted_at": null,
          "created_at": "2022-10-04T22:26:39.000000Z",
          "updated_at": "2022-10-04T22:26:39.000000Z"
        },
        {
          "id": 11,
          "admin_tickets_id": 1,
          "users_id": null,
          "admin_users_id": 1,
          "reply": "test",
          "deleted_at": null,
          "created_at": "2022-10-04T22:27:00.000000Z",
          "updated_at": "2022-10-04T22:27:00.000000Z"
        },
        {
          "id": 12,
          "admin_tickets_id": 1,
          "users_id": null,
          "admin_users_id": 1,
          "reply": "deneme",
          "deleted_at": null,
          "created_at": "2022-10-04T22:28:45.000000Z",
          "updated_at": "2022-10-04T22:28:45.000000Z"
        },
        {
          "id": 13,
          "admin_tickets_id": 1,
          "users_id": null,
          "admin_users_id": 1,
          "reply": "yine deneme",
          "deleted_at": null,
          "created_at": "2022-10-04T22:32:34.000000Z",
          "updated_at": "2022-10-04T22:32:34.000000Z"
        }
      ]
    }
  ]
}

但我希望它是这样的:注意消息下方的文件。

{
  "ticket": [
    {
      "id": 1,
      "subject": "test 1",
      "department_id": 1,
      "priority": "low",
      "status": "open",
      "users_id": 1,
      "created_at": "2022-10-03T11:47:42.000000Z",
      "updated_at": "2022-10-04T17:31:10.000000Z",
      "deleted_at": null,
      "author": "Admin Admin",
      "author_id": 1,
      "department": "Genel",
      "messages": [
        {
          "id": 1,
          "admin_tickets_id": 1,
          "users_id": 1,
          "admin_users_id": null,
          "reply": "test mesajıdır.",
          "deleted_at": null,
          "created_at": "2022-10-03T11:47:42.000000Z",
          "updated_at": "2022-10-03T11:47:42.000000Z"
          "attachments": [
                    {
                          "id": 1
                          "name": "deneme",
                          "extension": "pdf",
                    },
           ]
        },
        {
          "id": 5,
          "admin_tickets_id": 1,
          "users_id": null,
          "admin_users_id": 1,
          "reply": "test mesajıdır.",
          "deleted_at": null,
          "created_at": "2022-10-03T11:47:42.000000Z",
          "updated_at": "2022-10-03T11:47:42.000000Z"
        },
        {
          "id": 9,
          "admin_tickets_id": 1,
          "users_id": null,
          "admin_users_id": 1,
          "reply": "test için yaziyom",
          "deleted_at": null,
          "created_at": "2022-10-04T22:19:32.000000Z",
          "updated_at": "2022-10-04T22:19:32.000000Z"
        },
        {
          "id": 10,
          "admin_tickets_id": 1,
          "users_id": null,
          "admin_users_id": 1,
          "reply": "test",
          "deleted_at": null,
          "created_at": "2022-10-04T22:26:39.000000Z",
          "updated_at": "2022-10-04T22:26:39.000000Z"
        },
        {
          "id": 11,
          "admin_tickets_id": 1,
          "users_id": null,
          "admin_users_id": 1,
          "reply": "test",
          "deleted_at": null,
          "created_at": "2022-10-04T22:27:00.000000Z",
          "updated_at": "2022-10-04T22:27:00.000000Z"
        },
        {
          "id": 12,
          "admin_tickets_id": 1,
          "users_id": null,
          "admin_users_id": 1,
          "reply": "deneme",
          "deleted_at": null,
          "created_at": "2022-10-04T22:28:45.000000Z",
          "updated_at": "2022-10-04T22:28:45.000000Z"
        },
        {
          "id": 13,
          "admin_tickets_id": 1,
          "users_id": null,
          "admin_users_id": 1,
          "reply": "yine deneme",
          "deleted_at": null,
          "created_at": "2022-10-04T22:32:34.000000Z",
          "updated_at": "2022-10-04T22:32:34.000000Z"
        }
      ]
    }
  ]
}

我分享我的控制器和模型:TicketsController.php

public function show($id) {;
        $data = array();
        $data['ticket'] = AdminTickets::addSelect([
            'author' => Users::select(DB::raw("name || ' ' || surname as fullname"))
                ->whereColumn('id', 'admin_tickets.users_id')
                ->limit(1),
            'author_id' => Users::select('id')
                ->whereColumn('id', 'admin_tickets.users_id')
                ->limit(1),
            'department' => AdminTicketDepartments::select('name')
                ->whereColumn('id', 'admin_tickets.department_id')
                ->limit(1),
        ])->where('id', $id)->with('messages', fn ($query) =>
            $query->whereHas('attachments')
        )->get();
        return $data;
        die();
        return view('admin.tickets.show', $data);
    }

AdminTickets Model:

public function messages()
    {
        return $this->hasMany(AdminTicketReplies::class);
    }

AdminTicket 回复 Model:

public function attachments() {
        return $this->hasManyThrough(AdminTicketAttachments::class, AdminTicketReplies::class);
    }

我想我在 AdminTicketReplies model 中犯了一个错误。我该怎么做?

您现在会收到至少包含一个附件的所有邮件。 您可以使用另一个with()添加附件:

->where('id', $id)->with('messages', fn ($query) =>
            $query->whereHas('attachments')
            $query->with('attachments')
        )->get();

如果您想要所有邮件并包括附件,您可以使用点表示法:

->where('id', $id)->with('messages.attachments')

暂无
暂无

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

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