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
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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>
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.