Luís Almeida
Luís Almeida

Reputation: 183

Update field in a Column on Laravel

I'm trying to update the status on my tickets table to the value : 2. Once I can create the comment... (is working.. :) ), I wanted to change the status to 2.

This is my ticket model and the following function:

 public function addComment($id,$body,$solved)
{    

    $this->find($id)->status = 2;
    $this->save();            


    $this->comments()->create([
        'ticket_id' => $id,
        'body' => $body,
        'user_id' => auth()->id()
        ]);
}

Upvotes: 1

Views: 83

Answers (2)

Amr Aly
Amr Aly

Reputation: 3905

You need to get your object first then you can Update it:

public function addComment($id,$body,$solved)
{    
$ticket = $this->find($id);
$ticket->status = 2;
$ticket->save();            


$ticket->comments()->create([
    'ticket_id' => $id,
    'body' => $body,
    'user_id' => auth()->id()
    ]);
}

Upvotes: 2

Hollings
Hollings

Reputation: 532

Try changing your code like this. Maybe this will fix the problem you're having:

public function addComment($id,$body,$solved)
{    
    $ticket = Ticket::find($id);
    $ticket->status = 2;
    $ticket->save();            

    $ticket->comments()->create([
        'ticket_id' => $id,
        'body' => $body,
        'user_id' => auth()->id()
        ]);
}

Upvotes: 1

Related Questions