detach() method deleting more data

Asked

Viewed 103 times

0

my detach method erases all data from the student table (it removes the data from the association table but deletes all data from another table)

 $team = Team::find($id);              
 $team->students()->detach();            

 $team->delete();
 return redirect()->route('team.index')->with('message','Deletado com sucesso !!!');

my model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Team extends Model
{
    protected $fillable = ['name', 'nivel', 'series', 'period', 'school_id'];    
    protected $guarded = ['id' , 'created_at', 'update_at'];
    protected $table = 'teams';


    public function schools()
    {    
        return $this->belongsTo('App\Models\School'); 
    }

    public function students()
    {    
        return $this->belongsToMany('App\Models\Student',
                                    'student_team',
                                    'team_id',
                                    'student_id'); 
    }
}

Migration of the Students table

<?php

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

class CreateStudentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            $table->increments('id');
            $table->string('login'); 
            $table->string('password');
            $table->string('level_access');
            $table->string('full_name');
            $table->string('name');
            $table->string('last_name');            
            $table->date('birth_date');
            $table->string('birth_certificate');            
            $table->date('registration_date');#data de matricula
            $table->string('nationality');
            $table->string('ra');
            $table->string('rm');
            $table->string('race_color');            
            $table->string('rg')->nullable()->unique();
            $table->string('cpf')->nullable()->unique();
            $table->string('email')->nullable()->unique();                        
            $table->char('gender');
            $table->string('year');

            $table->string('phone');
            $table->string('phone2')->nullable();

            $table->string('city');
            $table->string('address');
            $table->string('district');
            $table->string('number');
            $table->string('zip_code');
            $table->unsignedInteger('state_id');
            $table->unsignedInteger('school_id');            
            $table->unsignedInteger('team_id');            

            $table->foreign('school_id')
                ->references('id')->on('schools')
                ->onDelete('cascade')
                ->onUpdate('cascade');

            $table->foreign('state_id')
                ->references('id')->on('states')
                ->onDelete('cascade')
                ->onUpdate('cascade');

            $table->foreign('team_id')
                ->references('id')->on('teams')
                ->onDelete('cascade')
                ->onUpdate('cascade');

            $table->rememberToken();    
            $table->timestamps();
            $table->engine = 'InnoDB';            
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('students');
    }
}

  • 1

    Which table it deletes that it could not?

  • it deletes data from Students table... should delete only data from student_team table

  • How are tables configured in your database? is not the command no.

  • am using on update Cascade, on delete

  • I’ll post my Migration

  • ->onDelete('cascade')->onUpdate('cascade'); you know what this command is for ???

  • update/delete daughter tables by updating/excluri parent table. but would Students be a daughter table? if it is the case did not know that when using association table she became daughter O.o

  • Well, I could help solve... I don’t know how to solve =/.

  • You gave this command $team->delete(); and has to do with students therefore excludes.

  • Noss, by vdd thank you so much. : D I’m novice here on the stack as I give like in your reply kkk

  • No response, just comments

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.