Home > Net >  laravel relationship tickets, ticket messages and message attachments
laravel relationship tickets, ticket messages and message attachments

Time:10-05

I am writing a support ticket system in laravel. I need to establish relationships between tables. I'm new to Laravel. I need to get the messages of a ticket and the files of those messages. By reading the documentation, I have established a relationship between the messages belonging to the ticket and it works correctly. However, I need to get the files under those messages, can you help me with this?

The json of the messages is as follows:

{
  "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"
        }
      ]
    }
  ]
}

but I want it to be like this: Pay attention to the file below the message.

{
  "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"
        }
      ]
    }
  ]
}

I share my controllers and models: 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);
    }

AdminTicketReplies Model:

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

I think I'm making a mistake in the AdminTicketReplies model. How can I do that?

CodePudding user response:

You are now getting all messages that have at least one attachment. you can add the attachments by using another with():

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

If you want all messages and include the attachments, you can use the dot notation:

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