method detach () deleting data over

0

My method detach deletes all data from the student table (it removes the data from the membership 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'); 
    }
}

Students table migration

<?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');
    }
}
    
asked by anonymous 02.05.2018 / 01:55

0 answers