Mauro
Mauro

Reputation: 372

Laravel is creating new record instead of updating old one

I have a planet overview and now I want to make that you can edit the planets, but instead of updating an existing database record, my Laravel creates a new database record. I looked on the internet and I saw the I wasn't the only one having issues with this and tried to solve it myself. I tried to add it all at once using

$member->update($request->only(['name','description', 'size_in_km', 'solar_systems_id']);

I've also tried to do it individually but nothing is working, when I submit the form, there are two planets in my database, a new planet with the entered input and the old one is also still there, instead of one planet with the new entered input.

PlanetController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Planet;
use App\Models\SolarSystems;

class PlanetController extends Controller
{
    public function home()
    {
        echo "Welcome at the home pagina!<br>
        <a href='planets'>Click here to see all the planets!</a>";
    }
    // Alle planeten weergeven
    public function index()
    {
        $planets = Planet::with('solar')->get();

        echo view('planets', ['planets'=>$planets]);
        echo "
        <a href='../planets/insert'>Insert new planets!</a>
        <br>
        <a href='/solarsystems'>Look at the solar systems!</a>
        ";
    }
    public function show($planeet)
    {
    // Alleen de gevraagde planeten weergeven
    $planets = Planet::with('solar')->where('name', $planeet)->get();
    echo view('planets', ['planets' => $planets]);
    echo "
    <a href='$planeet/edit'>Edit planet data</a><br>
    <a href='../planets'>Go back!</a>
    ";
    }
    public function insertForm()
    {
        $solarsystems = SolarSystems::all();
        return view('insertplanets', ['solarsystems' => $solarsystems]);
    }
    public function insertData(Request $request) {
        $name = $request->input('name');
        $description = $request->input('description');
        $size_in_km = $request->input('size_in_km');
        $solar_systems_id = $request->input('solar_systems_id');
        $data = array('name' => $name, 'description' => $description, 'size_in_km' => $size_in_km, 'solar_systems_id' => $solar_systems_id);
        Planet::insert($data);
        return redirect('./planets');
    }
    public function editPlanetDataForm($planeet)
    {
        $dataPlanet = Planet::where('name', $planeet)->first();
        echo view('editplanetdataform', ['dataPlanet' => $dataPlanet]);
    }
    public function editPlanetData($planeet, Request $request)
    {
        return Planet::where('name', $planeet)->first()->update([
        'name' => $request->input('name'),
        'description' => $request->input('description'),
        'size_in_km' => $request->input('size_in_km'),
        'solar_systems_id' => $request->input('solar_systems_id')
    ]);
    }
}

editplanetformdata.blade.php

<html>
    <head>
        <style>
            body {
                font-family: 'Nunito', sans-serif;
                padding: 0;
                margin: 0;
                text-align: left;
            }
        </style>
    </head>
    <body>
        <h2>Update data of the planet!</h2>
        <table>
            <form action='/planets/create' method='POST'>
            <input type = "hidden" name = "_token" value = "<?php echo csrf_token();?>"><input type = "hidden" name = "_token" value = "<?php echo csrf_token();?>"> 
            <tr>
                <th>New name of the planet</th>
                <td><input name='name' type='text' required></td>
                </tr>
                <tr>
                <th>New size in km of the planet</th>
                <td><input name='size_in_km' type='number' required></td>
                </tr>
                <tr>
                <th>New id of the solar system</th>
                <td><input type='number' name='solar_systems_id' required></td>
                </tr>
                <tr>
                <th>New information about the planet</th>
                <td><textarea rows='5' cols='40' name='description' required></textarea></td>
                </tr>
                <tr>
                <th><input type='submit' value='Add planet'></th>
                </tr>
            </form>
        </table>
        <a href='../../planets'>Go back!</a>
    </body>
</html>

Upvotes: 0

Views: 924

Answers (1)

kulas
kulas

Reputation: 41

public function editPlanetData($planeet, Request $request)
{
    return Planet::where('name', $planeet)->update([
    'name' => $request->input('name'),
    'description' => $request->input('description'),
    'size_in_km' => $request->input('size_in_km'),
    'solar_systems_id' => $request->input('solar_systems_id')
]);

Upvotes: 1

Related Questions