0

I have a laravel project with a one to one relationship. But I have a problem, I can't delete the data. The code looks perfectly normal and no errors appear, but it can't delete the data

this is my table structure

Teacher table

  • id
  • name
  • username
  • nip

Kelas table

  • nama_kelas
  • username
  • teacher_id

Migration kelas table :

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateKelasTable extends Migration
{
    public function up()
    {
        Schema::create('kelas', function (Blueprint $table) {
            $table->id();
            $table->string('nama_kelas');
            $table->string('username')->unique();
            $table->foreignId('teacher_id')->constrained()->cascadeOnDelete();
            $table->timestamps();
        });
    }
}

Migration teacher table :

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTeachersTable extends Migration
{
    public function up()
    {
        Schema::create('teachers', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('username')->unique();
            $table->bigInteger('nip')->unique()->nullable();
            $table->timestamps();
        });
    }
}

Teacher Model :

<?php

namespace App\Models;

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

class Teacher extends Model
{
    use HasFactory;

    protected $guarded = ['id'];

    public function kelas()
    {
        return $this->hasOne(Kelas::class);
    }

    public function getRouteKeyName()
    {
        return 'username';
    }
}

Kelas Model :

<?php

namespace App\Models;

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

class Kelas extends Model
{
    use HasFactory;

    protected $guarded = ['id'];

    public function teacher()
    {
        return $this->belongsTo(Teacher::class);
    }

    public function getRouteKeyName()
    {
        return 'username';
    }
}

Kelas Delete Controller :

public function destroy(Kelas $kelas)
{
    $kelas->delete();

    return redirect('/kelas');
}

Button Delete :

<form action="{{ route('kelas.destroy', $k->username) }}" method="POST" class="d-inline">
    @method('DELETE')
    @csrf
    <button onclick="return confirm('Are you sure you want to delete this post?')">
        Delete
    </button>
</form>
miken32
  • 42,008
  • 16
  • 111
  • 154
  • 1
    Is ```$k->username``` the id because if it is not than you will not get instance using dependency injection in Kelas delete controller ```public function destroy(Kelas $kelas)``` – geeky Jan 17 '22 at 18:27
  • 1
    Can you show us your route? – Don't Panic Jan 17 '22 at 21:54
  • @miken32 I use route model binding. based on laravel documentation, if I want to use column other than id when receiving model class, then I need to override the getRouteKeyName method on my Eloquent model. https://laravel.com/docs/8.x/routing#customizing-the-default-key-name – Berliano Ridho Pratama Jan 18 '22 at 00:30
  • Oh sorry I didn’t notice you override the method in your class. You should show the route definition in your question. – miken32 Jan 18 '22 at 01:56

2 Answers2

-1

can you try this code

public function destroy(Kelas $kelas)
{
   $kelas->teacher()->delete(); // delete for relation 
   $kelas->delete(); // delete row in table
   return redirect('/kelas');
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 18 '22 at 13:42
-1

I do not think that cascading the deletion works on laravel as we expect it (correct me if Im wrong), however what you could do is use an observer on the model you want, and as soon as the deletion event is triggered then you automatically delete the relationships. take a look at the reply on this previously asked question:

Automatically deleting related rows in Laravel (Eloquent ORM)

From laravel documentation: https://laravel.com/docs/8.x/eloquent#observers

Marwane Ezzaze
  • 1,032
  • 5
  • 11