skip to Main Content

I’m trying to setup a simple button to update a db column value when clicked. I can’t seem to figure out why my route isn’t getting passed my value however?

HTML:

<form method="post" action="{{ route('approveResturant') }}">
  {{ csrf_field() }}
  <input type="hidden" name="id" value="{{ $resturant->id }}" />
  <button class="btn btn-outline-success" type="submit">
    Approve
  </button>
</form>

Controller:

public function approveResturant($request)
    {
        dd($request->all());
        $id = $request->id;
        $resturant = Resturant::find($id);
        $resturant->approved = 1;
        $resturant->save;

        return redirect()->back()->with('message', 'Resturant Approved Successfully!');
    }

Route:
Route::post('approveResturant'[ResturantController::class,'approveResturant'])->middleware(['auth'])->name('approveResturant');

And finally, the error itself:
Error Message

Any help appreciated!

3

Answers


  1. Add the Request type-hint to your function:

    use IlluminateHttpRequest;
    
    public function approveResturant(Request $request)
    {
        dd($request->all());
        $id = $request->id;
        $resturant = Resturant::find($id);
        $resturant->approved = 1;
        $resturant->save;
    
        return redirect()->back()->with('message', 'Resturant Approved Successfully!');
    }
    

    The difference here is that Laravel understands the Request type-hint and knows that it should inject the Request object from the pre-defined services it has in its service container. Otherwise, Laravel doesn’t know where that parameter is coming from so assumes you will provide it. Simply naming your parameter $request is insufficient.

    Update

    Do you know why the function would still not be saving the new approved value to the DB?

    A few potential reasons:

    1. You have not removed the dd($request->all()); statement
    2. $resturant = Resturant::find($id); failed to find a record in the database
    3. save is a function not a property so $resturant->save; should be $resturant->save();

    To isolate the exact issue you will need to perform some debugging (e.g. either using xdebug or dd statements).

    Login or Signup to reply.
  2. Use Request Class

    use IlluminateHttpRequest;
    
    public function approveResturant(Request $request)
    {
        dd($request->all());
        $id = $request->id;
        $resturant = Resturant::find($id);
        $resturant->approved = 1;
        $resturant->save;
    
        return redirect()->back()->with('message', 'Resturant Approved Successfully!');
    }
    
    Login or Signup to reply.
  3. <form method="post" action="{{ route('restaurant.approveResturant') }}">
      {{ csrf_field() }}
      <input type="hidden" name="id" value="{{ $resturant->id }}" />
      <button class="btn btn-outline-success" type="submit">
        Approve
      </button>
    </form>
    
    Route::post("/restaurant/store", [RestaurantController::class, "approveResturant"])->name("restaurant.approveResturant");
    
    use IlluminateHttpRequest;
    
    public function approveResturant(Request $request)
    {
        $restaurant = Restaurant::where("id", $request->input("id"))->update([
            "approved" => 1
        ]);
    
        return redirect()->back()->with('message', 'Restaurant Approved Successfully!');
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search