Home > Enterprise >  Laravel - Form value is not updating
Laravel - Form value is not updating

Time:06-23

I have a projects table with the value of the following column:

id_project   name           website         country_id  currency_id  timezone_id
==========.  ====           =======         ==========. ===========. ===========
1            project name.  www.yahoo.com.  10          8            10

I have form with above fields. Now, when I press submit I can see I can get updating form fields data using:

dd($request);

But using this code the database is not updating:

public function update(Request $request, $id)
{
    //dd($request); // It's showing me updated form fields value

    $project = Project::where('id_project', $id)->first();
    $project->name = $request->name;
    $project->country_id = $request->country;
    $project->currency_id = $request->currency;
    $project->timezone_id = $request->timezone;
    $project->website = $request->website;
    $project->save();

    return response()->json($project, 200);
}

Update:

Strange things. If I run this it's not updateing

$project = Project::where('id_project', $id)->update(['name' => $request->name ]);

But If I manually add the value:

$project = Project::where('id_project', $id)->update(['name' => 'example data' ]);

It's updating database.

Any Idea?

CodePudding user response:

Please, make sure your $id variable has any value in it. Also you could try converting this into DB Query Builder

public function update(Request $request, $id)
{
    DB::table('projects')->where('id_project', $id)->update([
         'name' => $request->name,
         'country_id' => $request->country,
         'currency_id' => $request->currency,
         'timezone_id' => $request->timezone,
         'website' => $request->website
    ]);
}

But first check if dd($id) and dd($request->all()) has any values.

CodePudding user response:

public function update(Request $request, $id)
{
    $project = Project::find($id);
    $project->name = $request->name;
    $project->country_id = $request->country;
    $project->currency_id = $request->currency;
    $project->timezone_id = $request->timezone;
    $project->website = $request->website;
    $project->save();
}

Try this!

CodePudding user response:

the best practice in laravel to update records it´s:

public function update(Request $request, $id)
{
    $project = Project::find($id);
    $project->name = $request->name;
    $project->country_id = $request->country;
    $project->currency_id = $request->currency;
    $project->timezone_id = $request->timezone;
    $project->website = $request->website;
    $project->save();
}

also yo can use this:

$data = array(
                'compania_id'        => $request->get("compania"),
                'nombre_tratamiento' => $request->get("nombre_tratamiento"),
                'precio_sesion'      => $request->get("precio_sesion"),
            );
            $tratamiento = TreatmentCompany::find($request->get("cod_tratamiento"));

            $result = $tratamiento->update($data);

            return $result;
  • Related