Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

UsmanBasharmal's avatar

Deleteing data from multiple tables depending relationship in Laravel Vuejs Api

i have two tables Company and Employee

both in relationship Company is parent tble and Employees is child table.

now i need to delete record from parent table which in turn must delete all the relevant child table.

how to do this??

Kindly some one suggest your ideas please.

it give me the below error

message: "No query results for model [App\Employee] 2"

Modal code is:

public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
            $table->bigIncrements('id');
             $table->string('BadgeCode')->unique();
              $table->integer('company_id');
              $table->timestamps();
        });
    }

API code is:

Route::apiResources(['company'=>'API\CompanyController']);

Code in CompanyController is:

 public function destroy($id)
    {
         $this->authorize('isAdmin');
        $user = Company::findOrFail($id);
        Employee::where('company_id',$id)->delete();
        $user->delete();
        return ['message'=>'Company Deleted Successfully'];
    }

My Company.Vue Scriptcode is:

deletecompany(id) {
      if (this.$gate.isAdmin()) {
        swal
          .fire({
            title: "Are you sure?",
            text: "You won't be able to revert this!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
            confirmButtonText: "Yes, delete it!"
          })
          .then(result => {
            //Send request to the server

            if (result.value) {
              this.form
                .delete("api/employee/" + id)
                .then(() => {
                  // swal.fire("Deleted!", "Your file has been deleted.", "success");
                  toast.fire({
                    type: "success",
                    title: "Your Selected Company is successfully deleted!"
                  });
                  Fire.$emit("refreshPage");
                })
                .catch(e => {
                  console.log(e);
                });
            }
          });
      } else {
        toast.fire({
          type: "error",
          title: "You don't permission to perform this action!"
        });
        Fire.$emit("refreshPage");
      }
    }

HTML Code is:

<a href="#" v-if="$gate.isAdmin()" @click="deletecompany(company.id)">
                      <i class="fa fa-trash red"></i>
                    </a>
0 likes
8 replies
goldtaste's avatar

Try something like:

$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');

I think this is the best place to handle the employee deletion

2 likes
ronon's avatar

I would go for the same solution as @goldtaste .

But if you just want to remove particular ones and change the parent you could go with the static::deleting event. You could then update the specific childs and remove the other ones, before the parent gets deleted

UsmanBasharmal's avatar

I am using relationships like below

public function Employees() { return $this->hasMany('App\Employee'); }

goldtaste's avatar

Also, looks like you are hitting the wrong api end point.

Think it should be

.delete("api/company/" + id)

not

.delete("api/employee/" + id)

And then for the destroy simply add:

public function destroy($id)
    {
        $this->authorize('isAdmin');
        $company = Company::findOrFail($id);
        $company->delete();
        return ['message'=>'Company Deleted Successfully'];
    }
UsmanBasharmal's avatar
UsmanBasharmal
OP
Best Answer
Level 3

I found the Solution first I removed the "$this->authorize('isAdmin');" and I have added these lines public function up() { Schema::create('employees', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('BadgeCode')->unique(); $table->bigInteger('company_id')->unsigned(); $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); $table->timestamps(); }); }

Please or to participate in this conversation.