@moham96 Dude, this is really bad. See my comments.
Schema::create('teachers', function (Blueprint $table) {
$table->id(); //Good
$table->timestamps(); //Good
$table->json('name'); //Why the hell do you use json here? use string 255 characters is enough.
$table->longText('email'); //Why use long text here use string 255 characters is enough
$table->json('degree'); //Why use json here, if more than one possible value, extract to another table.
$table->json('position'); //Why use json here, same as for the degree, extract if more than one.
$table->json('body'); //I guess this is some kind of summary, but text should be used,
$table->foreignId("branch_department_id")->constrained()->cascadeOnDelete()->cascadeOnUpdate();
});
I also suggest that you read this post.
https://tray2.se/posts/database-design
Adding index to visitable_type makes the sql query instant but the same code in PHP is still slow for some reason
Of course it does, share your php code and show us what you do in it.