0

In a Laravel context, I've got this messages page, with all the messages belonging to a specific user. Initially all messages are not readed, so I put a button to change the boolean in DB (from 0 to 1) and finally show the message.

I'm doing this:

The view

@if ($message->readed != 0)
  <p class="card-text message text-left">{{ $message->message }}</p>
@else
  <form method="POST" action="/message/read">
     @csrf 
     @method('PATCH')
     <input type="hidden" name="message" value="{{ $message->id }}"/>
     <button class="btn btn-info text-white" type="submit">
       Leggi
     </button>
  </form>
@endif

The route in web.php

Route::patch('message/read', 'MusicianController@readMessage');

The function

    public function readMessage(Request $request)
{
    $message = Message::where('id', $request->id)->first();
    $message->readed = 1;
    $message->update();

    return redirect()->back()->with('message', 'message updated');
}

But it's not working, as soon as I click the button to show the message (and even change the DB value) I've got this error: The PATCH method is not supported for this route. Supported methods: GET, HEAD.

Even if I had specified a patch method in routes and even in the form with @method('PATCH')

Could someone help me understand what's wrong please??

D.D.
  • 99
  • 10

1 Answers1

0

the main answer

your route is:

Route::patch('message/read', 'MusicianController@readMessage');

replace your route with following route that use for all CRUD opration:

Route::resource('message/read', 'MusicianController');

if you use ajax for submit data then replace your type and url with following:

type: "patch",
url: "{{url('message/read')}}",

if you don't use ajax than use following:

<form method="POST" action="{{url('message/read"')}}">
    {{csrf_field()}}
    {{ method_field('PATCH') }}
</form>

update: after version 5.6 you can use these syntax for above functions in any blade file:

<form method="POST" action="{{url('message/read"')}}">
    @csrf
    @method('PATCH')
</form>
hosam zaki
  • 56
  • 3