cleocoder
cleocoder

Reputation: 117

Laravel: Updating a value keeps value as null?

On a view page on Laravel, I am displaying information but I also have a form at the bottom of the view where users can leave a 'comment'. In the database table, the field for comments is initially set as 'null', and then if users decide to submit a comment it will update the field.

The code runs, however it does not seem to be working as when I check the database the value is still null?

Controller (update function):

 public function update(Request $request, $MealPlan_ID) {

    $comments = $request->comments;
    $commentupdate = MealPlanInput::where('id', '=', $MealPlan_ID)->update(['comments' => $comments]);

    $data = MealPlanInput::where('id', '=', $MealPlan_ID)->get();

return view('MealPlanDisplay.modal', compact('data', 'MealPlan_ID'));

Controller (the show function, threw an error when I did not have it):

public function show($MealPlan_ID) {
        $data = MealPlanInput::where('id', '=', $MealPlan_ID)->get();

      return view('MealPlanDisplay.modal', compact('data','MealPlan_ID'));
      }

The view with form:

  <form method="put" action="{{ route('MealPlanDisplay.update', $MealPlan_ID) }}">

    <div class="shadow overflow-hidden sm:rounded-md">
        <div class="px-4 py-5 bg-white sm:p-6">
            <label for="comments" class="block font-medium text-sm text-gray-700">Personal Comments about Meal Plan</label>
            <input type="text" name="comments" id="comments" type="text" class="form-input rounded-md shadow-sm mt-1 block w-full"
                   value="{{ old('comments', '') }}" />
            @error('comments')
                <p class="text-sm text-red-600">{{ $message }}</p>
            @enderror
        </div>

        <div class="flex items-center justify-end px-4 py-3 bg-gray-50 text-right sm:px-6">
            <button class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase">
                Submit Comment
            </button>

Route:

//the /view and then the relevant ID of the information being displayed
Route::put('/MealPlanDisplay/modal/{MealPlan_ID}', [MealPlanDisplayController::class, 'update']);

The one thing I noticed that the URL will change after submitting, so when the URL normally is: /MealPlanDisplay/2

If I submit a comment called 'Test', the URL changes to: /MealPlanDisplay/2?comments=Test

I'm confused as to what I'm doing wrong with the update (put)? Would highly appreciate some help.

Upvotes: 2

Views: 1271

Answers (1)

Aless55
Aless55

Reputation: 2709

For the sake of understanding, I transferred @nice_dev comment to your code. I also included the @csrf statement which adds the csrf token, you usually want it in post requests. (otherwise you might get some errors)

<form method="post" action="{{ route('MealPlanDisplay.update', $MealPlan_ID) }}">
  @method('PUT')
  @csrf
//.... the rest

Why do you need to use POST instead of PUT?
PUT is not recognized as a valid method, therefore a get request is sent to your backend and the wrong controller method is invoked.

Upvotes: 1

Related Questions