Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel can't Delete data, it can enter on destroy() function but it parameter return null

So i’m try to delete data on laravel using resource route. but the data remain and still return the function. Im also try to delete the Greens from destroy() parameter, it make $greens containing ‘id’. but still, i want to keep destroy() parameter to be (Greens $greens), not just ($greens)

Controller :

    public function destroy(Greens $greens)
    {
        //dd($greens->id);
        Greens::destroy($greens->id);
        return redirect('/greens');
    }

Form :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>


    <table>
        @foreach ($data as $d)
        <tr>
            <td>
                {{$d['brand']}}
            </td>
            <td>
                <form action="/greens/{{$d['id']}}" method="POST">
                    @method('DELETE')
                    @csrf
                    <button>Delete</button>
                </form>
            </td>
        </tr>
        @endforeach
    </table>


</body>

</html>

Model :

<?php

namespace App\Models;

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

class Greens extends Model
{
    use HasFactory;
    protected $primaryKey = 'id';
}

Route:

<?php

use App\Http\Controllers\GreensController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});
Route::resource('/greens', GreensController::class);

i try to var_dump $greens->id but the result is null

>Solution :

When you use Laravel Route Resource, there’s a format you should follow, refer to this link: https://laravel.com/docs/9.x/controllers#actions-handled-by-resource-controller

I can see here in your destroy function $greens is in plural, you should use the singlular form $green:

public function destroy(Greens $green)
{
    Greens::destroy($green->id);
    return redirect('/greens');
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading