简体   繁体   中英

PHP function updating all title fields for every task in whole table

I am trying to update the title as a single database task entry. My function currently updates the titles of all the tasks in the table.

public function update(User $user, Task $task, Request $request) {
    $data = $this->validate($request, [
        'title' => 'required|string|max:255',
    ]);

    Auth::user()->tasks()->update([
        'title' => $data['title'],
        'is_complete' => 0,
        'updated_at' => '2020-01-30 10:39:33',
        'created_at' => '2020-01-30 10:39:33'
    ]);

    session()->flash('status', 'Task Completed!');

    return redirect('/profile/' . auth()->user()->id);
}

Use where('id', $task->id) on Task eloquent builder:

Auth::user()->tasks()->where('id', $task->id)->update([
       'title' => $data['title'],
       'is_complete' => 0,
       'updated_at' => '2020-01-30 10:39:33',
       'created_at' => '2020-01-30 10:39:33'
])

Try this

 public function update(User $user, Task $task, Request $request)
    {
        $data = $this->validate($request, [
            'title' => 'required|string|max:255',
        ]);
        Auth::user()->tasks()->find($task->id)->update([
            'title' => $data['title'],
            'is_complete' => 0,
            'updated_at' => '2020-01-30 10:39:33',
            'created_at' => '2020-01-30 10:39:33',
        ]);
        session()->flash('status', 'Task Completed!');

        return redirect('/profile/' . auth()->user()->id);
    }

add find($task->id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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