#Can't update form field

47 messages · Page 1 of 1 (latest)

agile gull
#

For the past hour I haven't been able to figure out why I can't update a form of a model that I have and it's getting really annoying.

Web.php

Route::get('/listings/edit/{listing}', [ListingController::class, 'edit']);
Route::put('/listings/update/{listings}', [ListingController::class, 'update']);

any help would be greatly appreciated!

#

edit.blade.php

@extends('layout')
 
@section('content')
    <h1>Edit</h1>
<form method="POST" action="/listings/update/{{$listing->id}}">
    @csrf
    @method('PUT')

    <label for="title">Title:</label>
    <input type="text" name="title" id="title" value="{{$listing->title}}"><br>

    @error('title')
        <p>{{$message}}</p>
    @enderror
    <label for="tags">Tags:</label>
    <input type="text" name="tags" id="tags"  value="{{$listing->tags}}"><br>
    @error('tags')
        <p>{{$message}}</p>
    @enderror
    <label for="company">Company:</label>
    <input type="text" name="company" id="company" value="{{$listing->company}}" ><br>
    @error('company')
        <p>{{$message}}</p>
    @enderror
    <label for="location">Location:</label>
    <input type="text" name="location" id="location"  value="{{$listing->location}}"><br>
    @error('location')
        <p>{{$message}}</p>
    @enderror
    <label for="email">Email:</label>
    <input type="email" name="email" id="email" value="{{$listing->email}}"><br>
    @error('email')
        <p>{{$message}}</p>
    @enderror
    <label for="website">Website:</label>
    <input type="text" name="website" id="website"  value="{{$listing->website}}"><br>
    @error('website')
        <p>{{$message}}</p>
    @enderror
    <label for="description">Description:</label>
    <textarea name="description" id="description"  value="">{{$listing->description}}</textarea><br>
    @error('description')
        <p>{{$message}}</p>
    @enderror
    <button type="submit">Save</button>
</form>

@endsection

#

the controller functions

    public function edit(Listing $listing) {
        
        return view('listings.edit', [
            'listing' => $listing
        ]);
    }


    public function update(Request $request, Listing $listing) {
        $formFields = $request->validate([
       'title' => 'required',
       'tags' => 'required',
       'company' => 'required',
       'location' => 'required',
       'email' => ['required'],
       'website' => 'required',
       'description' => 'required',
           ]);
           $listing->update($formFields);
   
           return back();
     }
simple spire
#

Hey 👋

#

to help, we could do with knowing about what error you see (if you see one)

south bridge
#

you should edit the code snippets to add highlighting

agile gull
#

Sorry!

Basically, i see and have no errors, i get redirected to the page like it should without the data being updated

#

When i dump the submitted data i see the changed data

#

The problem is at $listing->update as it returns false when dumped but i cant figure out why

simple spire
#

on your model

#

do you have protected $guarded = []; or a protected $fillable = [/* a list of your fields here */]; that might be the problem?

agile gull
#

I do have a fillable trait in my model with the desired attributes

simple spire
#

Fillable Trait or property?

#

If they're set as fillable, just make sure that all the fields you want to update are there and spelled correctly first of all.

#

Then another test might be to comment that out and replace it with protected $guarded = [] and see if that fixes it , if it does, it's something to do with $fillable

#

another thing to check is to call dd($request->all()); at the top of your controller's update() method to make sure the input is coming through into the controller ok.

agile gull
#

Sorry for the late reply, here's the model


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Listing extends Model
{
    use HasFactory;

    protected $fillable = ['title', 'company', 'tags', 'description',
    'website', 'email', 'location'];


}

simple spire
#

Also, check that the order of arguments in your update function isn't messing things up

#

it might be that you need to have:

#

public function update(Listing $listing, Request $request) {

#

instead of
public function update(Request $request, Listing $listing) {

#

let me know either way, i'm curious now 🙂

agile gull
#

Hey thanks for the quick reply

#

i just tried to create a listing instead of updating at it creats a new instance when i submit the dit form

#

will try the function

#

nope

#
  public function update(Listing $listing, Request $request) {
        $formFields = $request->validate([
       'title' => 'required',
       'tags' => 'required',
       'company' => 'required',
       'location' => 'required',
       'email' => ['required'],
       'website' => 'required',
       'description' => 'required',
           ]);
           $listing->update($formFields);
   
           return back();
     }
simple spire
#

Did you use dd to dump out what's coming through in the input ?

#

best way to debug things like this is in stages:

  • is my data being sent successfully from the form
  • is my data reaching the controller in the right format/intact
  • is validation causing an issue
  • is saving an issue (fillable/protected)
agile gull
#

if i dump die the form fields the changed fields show up like expected

#

when i dump the $listing->update it shows false

#

as in, it can't update

#

maybe it has to do with the request?

simple spire
#

Do you have any observers or ::creating or ::saving events attached to the listing model anywhere?

#

if it couldn't save, it should REALLY throw an exception with a reason, unless false is being returned in an observer somewhere

#

can you try dd ing the $listing model at the top of your controller and pasting me a screenshot?

agile gull
#

@simple spire

simple spire
#

Yeah ,see how exists => false

#

it means it's loading a blank instance of the model, not the one you're trying to update

#

that's why update is coming back false

#

one second...

#

Your form action looks fine, can you check that {{$listing->id}} is printing out in your blade template?

delicate escarp
agile gull
#

that was it

#

thanks so much