Courses

How to Create Laravel 7 API

Finishing CRUD: Update, Delete and Resource Controller

Previous: Validation, Errors and Status Codes
avatar

Hello, What if we have a relation in the products table to some category? Should we add in our migration some onDelete, onUpdate events? I got an error like this:

{"message": "SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`laravel_api`.`products`, CONSTRAINT `products_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`)) (Connection: mysql, SQL: delete from `categories` where `id` = 2)"}
avatar

It depends on what you want to do on that update/delete events. One of the ways is to have ->cascadeOnDelete() at the end, so it would delete the related records.

avatar

those constant from symphony i check the documentation in laravel but they are not mentionned there .

avatar
You can use Markdown
avatar

I have a question one of you recommendations is not to pass " $request->all() " in order to create because is not unsafe ?

avatar

Correct, here's an article about it: //post/laravel-request-all-security-issue

avatar

thank you

avatar
You can use Markdown
avatar
You can use Markdown