mercredi 19 décembre 2018

Laravel, Detach() method to delete parent records

Hi guys I'm working with many to many relationship and I want to know if is there any way to delete the records of the main table.

These are my tables

  Schema::create('inventario_inicial', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('producto_nombre_id')->unsigned();
        $table->foreign('producto_nombre_id')->references('id')->on('producto_nombre');
        $table->integer('existencias');
        $table->double('promedio');
        $table->timestamps();
    });
    Schema::create('empresa_inventario_inicial', function (Blueprint $table) {
        $table->integer('empresa_id')->unsigned();
        $table->foreign('empresa_id')->references('id')->on('empresas');
        $table->integer('inventario_inicial_id')->unsigned();
        $table->foreign('inventario_inicial_id')->references('id')->on('inventario_inicial');
    });

I can get the data via pivot with this code

$empresa = Empresa::find($request->empresa_id);
$empresa->inventario_inicial(); 

To detach the data of that $empresa i use $empresa->inventario_inicial()->detach();

It deletes the records of the pivot table witch is correct, but also I want to delete not only what's in empresa_inventario_inicial but also inventario_inicial that were related. Something like cascade deleting but from pivot table.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire