ojwando's avatar

Symfony \ Component \ The GET method is not supported for this route. Supported methods: PATCH.

My route for update is this: ``` Route::patch('/questions/{question}/update-question', [QController::class,'update'])->name('questions.update');

My controller is this:    

public function update(Request $request, queations $question) { if($request->hasFile('image')) { // Get filename with extension $filenameWithExt = $request->file('image')->getClientOriginalName(); // Get just filename $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME); // Get just ext $extension = $request->file('image')->getClientOriginalExtension(); //Filename to store $fileNameToStore = $filename.'_'.time().'.'.$extension; // Upload Image $path = $request->file('image')->storeAs('public/images', $fileNameToStore); } else { $fileNameToStore = ''; }

    $postData = $request->all();
    // dd($postData);

    // Update properties of the $post model with data from $postData
    $question->statement = $postData['statement'];
    
    $question->topic = $postData['topic'];
    $question->subject = $postData['subject'];
    $question->answer = $postData['answer'];
    // ... (update other fields as needed)

    // Save the changes to the database
    

    $question->image = $fileNameToStore;
    $question->save();
    return redirect('/')->with('success', 'question updated successfully');  



}
My edit.blade.php
<div class="form-group">
    <label for="topic">Topic Name</label>
    <input class="form-control" type="text" name="topic" placeholder="Title" value="{{ $question->topic }}">
</div>

<div class="form-group">
    <label for="subject">Subject</label>
    <input class="form-control" type="text" name="subject" placeholder="Title" value="{{ $question->subject }}">
</div>

<div class="form-group">
    <label for="statement">Statement</label>
    <textarea name="statement" id="statement" rows="5" class="form-control">{{ $question->statement }}</textarea>
</div>

<div class="form-group">
    <label for="answer">Answer</label>
    <textarea name="answer" id="answer" rows="5" class="form-control">{{ $question->answer }}</textarea>
</div>

<div class="form-group">
    <label for="image">Image</label>
    <input type="file" name="image" placeholder="Title" value="{{ $question->image }}">
</div>

<button type="submit" class="btn btn-success">Update</button>

@endcan

ClassicEditor
.create(document.querySelector('#statement'))
.catch(error => {
    console.error(error);
});

ClassicEditor .create(document.querySelector('#answer'))

.catch(error => {
    console.error(error);
});

@endsection

0 likes
15 replies
tangtang's avatar

@ojwando

where the <form> tag and method ?

this is for sample

<form method="POST" action="{{ route('questions.update', $question->id) }}" enctype="multipart/form-data">
    @csrf
    @method('PATCH')
    <!-- your form fields and content go here -->
    <button type="submit" class="btn btn-success">Update</button>
</form>
ojwando's avatar

@tangtang I did copy properly though I have the form:

<form  action="{{ route('questions.update', $question) }}" method="POST" enctype="multipart/form-data">
    @csrf
    @method('PATCH')

    <div class="form-group">
        <label for="topic">Topic Name</label>
        <input class="form-control" type="text" name="topic" placeholder="Title" value="{{ $question->topic }}">
    </div>

    <div class="form-group">
        <label for="subject">Subject</label>
        <input class="form-control" type="text" name="subject" placeholder="Title" value="{{ $question->subject }}">
    </div>

    <div class="form-group">
        <label for="statement">Statement</label>
        <textarea name="statement" id="statement" rows="5" class="form-control">{{ $question->statement }}</textarea>
    </div>

    <div class="form-group">
        <label for="answer">Answer</label>
        <textarea name="answer" id="answer" rows="5" class="form-control">{{ $question->answer }}</textarea>
    </div>

    <div class="form-group">
        <label for="image">Image</label>
        <input type="file" name="image" placeholder="Title" value="{{ $question->image }}">
    </div>

    <button type="submit" class="btn btn-success">Update</button>
</form>

@endcan
 
  </div>
   </div>
  @section('script')
  
    ClassicEditor
    .create(document.querySelector('#statement'))
    .catch(error => {
        console.error(error);
    });

ClassicEditor
    .create(document.querySelector('#answer'))
       
    .catch(error => {
        console.error(error);
    });

   
  @endsection

tangtang's avatar

@ojwando

the code seems fine.

but in this update function. there is a writing error in the question parameter

public function update(Request $request, queations $question)

is this error happen when user click the submit button or when accessed the edit form ?

ojwando's avatar

@tangtang

update works sometimes and sometimes it doesn't especially when text is long it doesn't

ojwando's avatar

@tangtang The error occurs when i click the submit button. However, when i use route::match(), i get array key statement null and when i dd($postData); i get empty array []

tangtang's avatar

@ojwando

update works sometimes and sometimes it doesn't especially when text is long it doesn't.

well, this odd. can you to dd in the first line after function update to check if this sumbit can reach the controller especially with long text as you mentioned ?

when i use route::match(), i get array key statement null and when i dd($postData); i get empty array []

even with no long text ? still empty array ?

is there another route have similar naming convention like this route ?

shariff's avatar

Everything looks fine to me. Can you show your routes file? Try clearing cache php artisan config:cache or php artisan route:clear

ojwando's avatar

@shariff

Route::patch('/questions/{question}/update-question', [QController::class,'update'])->name('questions.update');

ojwando's avatar

The update happens selectively when I don't embed an image or table. It does not update when I have a long text as well. But update happens when the text is short and no image embedded

ojwando's avatar

My rich text editor did not work, and my text were rendered with HTML. I have realized that update works when I remove the HTML attributes like classes and inline styles. Why is this?

ojwando's avatar

But I need class attribute to have a responsive bootstrap table

DhPandya's avatar

@ojwando How many problems you are dealing with here? is it for the reach text editor or is it with the update route? Be clear with your problem, Please.

Please or to participate in this conversation.