Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

464
Views
How to update on relevant fields in Laravel

I have a courses table and it has 4 fields namely - id, title, description, deadline.

I want to update only description and deadline, but what i have currently done queries the table by its id and updates all the fields.

How can i update only my description and deadline fields?

public function editCourse(Request $request, $id){
         // first check whether a course in that id exists to be updated
         $course = CourseModel::find($id);

         // if there is no such course
         if(is_null($course)){
             return response()->json(["message" => "Record Not found!"],404);
         }
 
         // update the database
         $course->update($request->all());
         return response()->json($course,200);
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can use save() method for updating certain field :

public function editCourse(Request $request, $id){
         // first check whether a course in that id exists to be updated
         $course = CourseModel::find($id);
         // if there is no such course
         if(is_null($course)){
             return response()->json(["message" => "Record Not found!"],404);
         }
         // update only description and deadline
         $course->description = $request->description;
         $course->deadline = $request->deadline;
         $course->save();
         return response()->json($course,200);
}
over 4 years ago · Santiago Trujillo Report

0

There are a couple different ways to do this:

  1. Use the only method on the $request.
    Course::where('id', $id)->update($request->only(['description', 'deadline']));
  1. You can load up the model, alter its fields manually and save it, like:
    $course = CourseModel::find($id);
    $course->description = $request->input('description');
    $course->deadline = $request->input('deadline');
    $course->save();

Both of those answers assume you are receiving the fields description and deadline in your request.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!