I
I
ilya173922019-08-24 21:46:32
Laravel
ilya17392, 2019-08-24 21:46:32

What is the path to write in the update route to get to the page with the update form?

Routes

Route::get('/edit/{id}', '[email protected]')->name('adEdit');

Route::get('/edit/{id}', '[email protected]')->name('adUpdate');

Throws SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null (SQL: update `ad` set `title` = ?, `description` = ?, `author_name` = momomo, `user_id` = 27 , `ad`.`updated_at` = 2019-08-24 18:42:31 where `id` = 48) because it doesn't redirect to the post update form page.
The form from update.blade.php file:
<form method="post" class="col-md-6" action="{{ route(‘update’, $oneAd->id) }}">

     <div class="form-group">
         <label>Title</label>
         <input  type="text" name="title" class="form-control">
         @error ('title')
         <small class="form-text text-danger">{{ $message }}</small>
         @enderror
     </div>
     <div class="form-group">
         <label>Description</label>
         <textarea type="text" name="description" class="form-control"></textarea>
         @error ('description')
         <small class="form-text text-danger">{{ $message }}</small>
         @enderror
     </div>
     <button type="submit" class="btn btn-primary">Save</button>
     @csrf
 </form>

Controller methods:
public function edit($id)
    {
        $ad = Ad::find($id);
        
        return view('update', [
            'ad' => $ad
            ]);
    }

    
    public function update(Request $request, $id)
    {
        
        
        $oneAd = Ad::find($id);
         
       $id = $oneAd->update([
        'title' => $request->title,
        'description' => $request->description,
        'user_id' => Auth::user()->id,
        'author_name' => Auth::user()->username
        
       ])->id;
        return redirect("/");
    }

and a button on the ad display page
<a href="/edit/{{ $oneAd->id }}" class="btn btn-primary">Update</a>

after clicking on which the record is immediately saved, but does not go to the page with the form

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vadim Milevsky, 2019-08-25
@ilya17392

You have both routes have the same path, so the last one works (he overwrote the first one).
To solve the problem, first attribute the following uri - /edit/{id}/update
And everything will work

E
evgenin, 2019-08-24
@evgenin

https://laravel.com/docs/5.8/controllers#resource-...

M
Maxim Shevchenko, 2019-08-25
@MarsianinM

Another option is to change the method for update
And in the form, insert This method, as for me, is the most correct, as it meets the logic @method('PATCH')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question