Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chris J's avatar

PUT request is not updating the database

Hello guys. I have a table with2 foreign keys and I want to be able to update data thru a form. So far I got in my view:

<form action="{{url('formInfo.edit/'.$testInfo->ID) }}" method="POST" style="display: grid">
                    @method('PUT')
                    @csrf
                    <input type="text" name="Name" value="{{ old('NAME') ?? $testInfo->NAME}}" placeholder="Name" class="form-control" style="margin-bottom: 8px">
                    <input type="text" name="Time" value="{{ old('TIME') ?? $testInfo->TIME}}" placeholder="Time" class="form-control" style="margin-bottom: 8px">
                    <select name='guys_id' class='form-control mt-2 mb-2'>
                        @foreach($guys as $guy)
                            <option value="{{ $guy->ID }}">{{ $guy->NAME}}</option>
                        @endforeach
                    </select>
                    <select name='LOCATION_ID' class='form-control mt-2 mb-2'>
                        @foreach($locations as $location)
                            <option value="{{ $location->id }}">{{ $location->Location_name}}</option>
                        @endforeach
                    </select>
                    <button type="submit" class="btn btn-primary">Post</button>
                </form>

In my controller I have this update function:

public function update(Request $req, $id){
        locations::all();
        observer::all();
        $ID = $req->ID;
        $input = $req->all();
        $testInfo = testInfo::find($id);
        $testInfo->NAME = $input['Name'];;
        $testInfo->TIME = $input['Time'];;
        $testInfo->guys_ID = observer::where('ID','=',$req->guy_id)->get('ID');;
        $testInfo->LOCATION_ID = locations::where('id','=',$req->LOCATION_ID)->get('id');;
        $testInfo->save();
        return redirect()->route('testInfo');
    }

in my Model:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class testInfo extends Model
{
    protected $table = "infotest";
    public $timestamps = false;
    protected $fillable=['ID','NAME','TIME', 'guys_ID', 'LOCATION_ID'];
}

And in my routes:

Route::put('formInfo.edit/{ID}', 'App\Http\Controllers\testInfoController@update');

My issue is that when I submit the form I get redirected to the correct page and get 302 Found in my network panel but table is not updated.

0 likes
7 replies
Sinnbeck's avatar

Can I suggest you watch a few videos and learn a bit about naming in laravel (and php in general) ? :) It will make things easier for you down the line. Here is also a guide you can reference: https://github.com/alexeymezenin/laravel-best-practices#follow-laravel-naming-conventions

What are these two doing? Just getting data to not use it?

        locations::all();
        observer::all();

And you send the $id as part of the url, but you also get it from the request (an input) ?

$ID = $req->ID;

Anyways. Use dd() to debug. For instance

$testInfo = testInfo::find($id);
dd($testinfo);

1 like
chris J's avatar

@Sinnbeck I have watched videos for laravel and php but this is something that I started so I can test if it works in order to make relationships to my DB tables. I understand this is not a good way of doing things. These lines

locations::all();
observer::all();

and also the id that I tried to get it from the request, I added them in order to understand why wasn't the table updating because at first I was getting a 200 response but nothing changed inside the table. I will use dd() as you suggested and read the guide that you posted. Thank you

Snapey's avatar

You could do with cleaning up your code and introducing some consistency, in particular with lettercase.

these lines do nothing, so should be deleted;

        locations::all();
        observer::all();

remove all the double semicolons

consistently name your fields (suggest lower case), for instance you have 'Name' and 'guys_id' and 'LOCATION_ID' all in the same form.

Don't needlessly create temporary variables when you can just access the original object

$input = $req->all();

Your form select is called 'guys_id' but you try and use it as 'guy_id'

All these inconsistencies distract me from the real problem

1 like
Snapey's avatar

Change your route;

Route::put('formInfo.edit/{id}', 'App\Http\Controllers\testInfoController@update')

Your update method

    public function update(Request $request, $id)
    {
        $testInfo = testInfo::find($id);

        $testInfo->NAME = $request->Name;
        $testInfo->TIME = $request->Time
        $testInfo->guys_ID = $request->guys_id;
        $testInfo->LOCATION_ID = $request->LOCATION_ID;
        $testInfo->save();

        return redirect()->route('testInfo');
    }

you should change your model name to follow convention for classes, so it should be Testinfo and Observer and Location (singular)

Hopefully you can see how much cleaner this reads with simple changes

Think about Validation and Authorization

1 like
chris J's avatar

@Snapey Thank you for your advice. I will try to clean my code as much as possible and I name my fields wit the way that you suggested me

chris J's avatar

I made some changes and starting watching the lessons that you recommended (didn't change the table names neither the column names in order to much laravel "rules") . My issue is caused because find($id) returns a collection instead of one element.

public function update(Request $req, $id){
        $input = $req->all();
        $testInfo = testInfo::find($id);
        $testInfo->NAME = $input['Name'];;
        $testInfo->TIME = $input['Time'];;
        $testInfo->OBSERVER_ID = $req->observer_id; 
        dd($id,testInfo::find($id),$testInfo->NAME = $input['Name'],$testInfo->TIME = $input['Time'],$testInfo->OBSERVER_ID = $req->observer_id, $testInfo->LOCATION_ID = $req->LOCATION_ID);
        $testInfo->LOCATION_ID = $req->LOCATION_ID;
        $testInfo->save();
    }

The result of the dd() is

"4"
App\Models\testInfo {#284 ▼
  #table: "infotest"
  +timestamps: false
  #fillable: array:5 [▶]
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: array:5 [▶]
  #original: array:5 [▶]
  #changes: []
  #casts: []
  #classCastCache: []
  #attributeCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}
"GEORGE"
"18:30"
"1"
"2"

How can I prevent find from creating a collection and return one element?

Please or to participate in this conversation.