0

How to perform delete in cascate? I have two objects, people and contacts.

One people have many contacts, when i delete the people need to delete all contacts, how to do this?

function for delete

public function destroy($id)
{
    $people= $this->ModelPeople->find($id);

    $people= $people->delete();
}

Migration People

public function up()
{
    Schema::create('Peoples', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name',100);
        $table->integer('sex');
        $table->integer('active');

        $table->timestamps();
    });
}

Migration Contact

public function up()
{
         Schema::create('contacts', function (Blueprint $table) {
            $table->increments('id');

         $table->integer('idpeople')->unsigned();
         $table->foreign('idpeople')->references('id')->on('people')->onDelete('cascade');

         $table->string('cel',20)->nullable();
         $table->string('email',100)->nullable();

         $table->timestamps();
     });
 }

Model People and Contact

    class People Model
    {
        protected $table ='Peoples';
        protected $fillable = ['name','sex','active']; 

        public function Contats(){
            return $this->hasMany(Painel\Contact::class);
        }
    }

    class Contat extends Model
    {
        protected $table = 'contacts';
        protected $fillable = ['idpeople','cel','email']; 

        public function people(){
            return $this->belongsTo(Painel\People::class);
        }

    }

with this scheme when deleting a person all contacts should be deleted as well? or is this not automatic and I who have to delete the contacts in my destroy method in the controller?

Bruno Silva
  • 147
  • 1
  • 11

0 Answers0