2

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.

cleocoder
  • 107
  • 5
  • 3
    method= put won't work. Use @method('put') instead and keep form method=post – nice_dev Aug 04 '21 at 16:43
  • 1
    and don't use raw queries. $plan = MealPlanInput::find($MealPlan_ID); $plan->comments = $request->comments; – Maksim Aug 04 '21 at 16:55
  • 1
    Does this answer your question? [Laravel form html with PUT method for PUT routes](https://stackoverflow.com/questions/28143674/laravel-form-html-with-put-method-for-put-routes) – nice_dev Aug 04 '21 at 16:55
  • 1
    @Maksim `::where('id', '=', $MealPlan_ID)` isn't a "raw" query, `DB::raw('SELECT * FROM meal_plans WHERE id = ' . $MealPlan_ID)` would be. I agree with encouraging usage of `::find()`, but `::where('id', $MealPlan_ID)->first()` also works. @cleocoder, as stated in an earlier question that you deleted, **stop using `->get()` for a single record**. `get()` returns a Collection of `MealPlanInput` instances, but only 1 will match on `id`, so use `::find($MealPlan_ID)` or `::where('id', $MealPlan_ID)->first();` – Tim Lewis Aug 04 '21 at 17:01
  • @TimLewis also work <> right design. Right way in this case is using model binding, not query in controller. – Maksim Aug 04 '21 at 18:21
  • 1
    @Maksim Yeah, Route Model Binding would handle that automatically, but that isn't being used here. Either way, like I said, I agree with usage of `::find()`. I was simply correcting that `::where(...)` is not a **raw** query; it's still using Eloquent. – Tim Lewis Aug 04 '21 at 18:23

1 Answers1

1

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.

Aless55
  • 2,652
  • 2
  • 15
  • 26