0
votes

I am working in Laravel5.4. I have created 3 table's.

User table :

enter image description here

tickets table :

enter image description here

ticket_cc_users table :

enter image description here

Now, I have create relation ship between users and tickets module like below.

User model :

public function tickets()
{
    return $this->belongsToMany('App\User', 'ticket_cc_users', 'user_id', 'ticket_id');
}

Ticket model :

public function users()
{
    return $this->belongsToMany('App\Models\Tickets\Ticket', 'ticket_cc_users', 'ticket_id', 'user_id');
}

TicketController controller save method :

public function store(Request $request)
{
    return $request->all();
    $ticket = new Ticket;
    $ticket->requester_id = $this->user['id'];
    //$ticket->assignee_id = $request->assignee_id;
    //$ticket->cc_id = $request->cc_id;
    $ticket->type = $request->type;
    $ticket->priority = $request->priority;
    $ticket->subject = $request->subject;
    $ticket->description = $request->description;
    $ticket->status = $request->status;
    if($request->link)
    {
        $ticket->link = $request->link;
        $ticket->due_date = null;
    }
    if($request->due_date && $request->due_date !="")
    {
        $ticket->due_date = date('Y-m-d',strtotime($request->due_date));
        $ticket->link = "";
    }
    if($ticket->save())
    {
        $ticket->users()->sync($request->cc_id);

        foreach($request->ticket_tags as $value){
            $tag = new Tag;
            $tag->tag_name = $value['text'];
            $tag->save();
            $ticketTag = new TicketTag;
            $ticketTag->tickets_id = $ticket->id;
            $ticketTag->tags_id = $tag->id;
            $ticketTag->save();
        }

        $data = Ticket::find($ticket->id);
        Mail::to('[email protected]')->send(new CreateTicket($data));

        $response = array(
            'success' => true
        );
    }
    return $response;
}

Here, I am going to store data into ticket table. So I need to store cc user data into ticket_cc_user table. So how can I store ticket_id and user_id into this table. Here, I can get multiple user_id. And I am using Eloquent ORM.

Here, It gives an error like SQLSTATE[42S22]: Column not found: 1054 Unknown column '$$hashKey' in 'field list' (SQL: insert into ticket_cc_users ($$hashKey, address, city_id, country_id, created_at, deleted_at, email, firstname, id, introducer_id, is_verified, lastname, phone, signature, state_id, ticket_id, updated_at, user_id, username, userrole_id) values (object:109, , , , 2017-02-10 05:26:01, , [email protected], , 26, 1, , , 9999999999, , , 1, 2017-02-14 08:33:18, 0, nisarg, 2))

So,what code should I have to change in save function to store data into ticket_cc_users table?

1

1 Answers

0
votes

It seems that belongsToMany() parameters order should be changed. The first and the last(4-th) parameters should be "about" same instance.

UPDATE Also the body of tickets() and users() functions should be exchanged

So try this:

User model :

public function tickets()
{
    return $this->belongsToMany('App\Models\Tickets\Ticket', 'ticket_cc_users', 'user_id', 'ticket_id');
}

Ticket model :

public function users()
{
return $this->belongsToMany('App\User', 'ticket_cc_users', 'ticket_id', 'user_id');        
}

From Laravel Many To Many Docs:

The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to:

return $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id');